Reputation: 33
I have a following XML data:
<Product>
<item>
<ProductVariant>
<item>
<VariantType>1</VariantType>
</item>
<item>
<VariantType>2</VariantType>
</item>
<item>
<VariantType>3</VariantType>
</item>
</ProductVariant>
<ChosenVariantType>2</ChosenVariantType>
</item>
</Product>
and than I have an xsl transformation:
<xsl:for-each select="Product/item/ProductVariant">
<xsl:if test="(item/VariantType = ../ChosenVariantType)">
<xsl:value-of name="test" select="item/VariantType"/>
<xsl:text>-</xsl:text>
<xsl:value-of name="testChosen" select="../ChosenVariantType"/>
</xsl:if>
</xsl:for-each>
which prints out: 1-2
so the question is why 'if' evaluates as true if VariantType is 1 and ChosenVariantType is 2 ?
Upvotes: 1
Views: 48
Reputation: 70618
You are iterating over ProductVariant of which there is only one in your XML. When you do your xsl:if condition, all you are testing is whether there is any item under the current ProductVariant with a matching VariantType. In your case, there is. But when you do the xsl:value-of, it will ouptut the value of the first item, whether it matches the variant type or not.
You could either change you xsl:value-of to this:
<xsl:value-of name="test" select="item[VariantType = ../ChosenVariantType]/VariantType"/>
(Although this is rather pointless because you know the VariantType matches ChosenVariantType).
Or maybe you need to iterate over item elements here?
<xsl:for-each select="Product/item/ProductVariant/item">
<xsl:if test="(VariantType = ../../ChosenVariantType)">
<xsl:value-of name="test" select="VariantType"/>
<xsl:text>-</xsl:text>
<xsl:value-of name="testChosen" select="../../ChosenVariantType"/>
</xsl:if>
</xsl:for-each>
Upvotes: 2