Home > OS >  XSLT for-each condition get elements based on element attribute value greater than 1
XSLT for-each condition get elements based on element attribute value greater than 1

Time:10-03

I'm sorry if this is duplicated question. I will delete it if it is duplicated. I am trying to transform XML file like example below, where I want to get variant of the sizes where only size with stock quantity > 1(before that i have to sum up all stocks to get the total quantity to be able to write condition). I have been trying with for-each, but I'm not sure where I did a mistake or how to correctly write the condition.

<?xml version="1.0" encoding="utf-8"?>
<offer>
    <products>
        <product>
            <sizes>
                <size id="B" name="38" available="in_stock" priority="5">
                    <price/>
                    <stock id="5" quantity="1" available_stock_quantity="1" />
                    <stock id="4" quantity="0" available_stock_quantity="0" />
                </size>
                <size id="C" name="38" available="in_stock" priority="5">
                    <price/>
                    <stock id="5" quantity="45" available_stock_quantity="45" />
                </size>
                <size id="D" name="38" available="in_stock" priority="5">
                    <price/>
                    <stock id="5" quantity="43" available_stock_quantity="43" />
                    <stock id="4" quantity="1" available_stock_quantity="1" />
                    <stock id="2" quantity="22" available_stock_quantity="22" />
                </size>
                <size id="E" name="38" available="in_stock" priority="5">
                    <price/>
                    <stock id="5" quantity="20" available_stock_quantity="20" />
                    <stock id="4" quantity="1" available_stock_quantity="1" />
                    <stock id="2" quantity="4" available_stock_quantity="4" />
                </size>
            </sizes>
        </product>
    </products>
</offer>

I have tried with selecting with for-each in XSLT file like below:

<xsl:for-each select="sizes/size/stock[@quantity > '1']">

and to sum stocks with this line

<xsl:element name="stocks">
   <xsl:value-of select="sum(.//stock/@available_stock_quantity)"/>
</xsl:element>

The result i want to get:

<variants groupingAttribute="size">
    <variant>
        <id>C</id>
        <size>38</size>
        <stocks>45</stocks>
        <prices/>
    </variant>
    <variant>
        <id>D</id>
        <size>38</size>
        <stocks>66</stocks>
        <prices/>
    </variant>
    <variant>
        <id>E</id>
        <size>38</size>
        <stocks>25</stocks>
        <prices/>
    </variant>
</variants>

CodePudding user response:

I want to get variant of the sizes where only size with stock quantity > 1

I am not sure I understand this condition. if you mean that you want to include a size only when at least one of its stock children has a quantity that is greater than 1, then this should be quite simple:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/offer">
    <variants groupingAttribute="size">
        <xsl:for-each select="products/product/sizes/size[stock/@quantity > 1]">
            <variant>
                <id>
                    <xsl:value-of select="@id" />
                </id>
                <size>
                    <xsl:value-of select="@name" />
                </size>
                <stocks>
                    <xsl:value-of select="sum(stock/@quantity)" />
                </stocks>
                <prices/>
            </variant>
        </xsl:for-each>
    </variants> 
</xsl:template>

</xsl:stylesheet>
  • Related