xslt - xsl:if-expression not working -



xslt - xsl:if-expression not working -

i got next structure:

<root> <elements> <element> <key>1</key> <key2>10</key2> </element> <element> <key>2</key> <key2>100</key2> </element> </elements> </root>

i want create block of

<newelement></newelement>

when illustration value of key 2 , value of key2 not ''

for selection of element xpath works perfectly:

/root/elements/element/key[text() = '2']/../key2[text() != '']

but when set xsl:if transformation won't create desired newelement

<xsl:if test="/root/elements/element/key[text() = '2']/../key2 != ''"> <newelement></newelement> </xsl:if>

what missing?

<root> <elements> <element> <key>2</key> <key2>100</key2> </element> <element> <key>2</key> <key2></key2> </element> </elements> </root>

xslt code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml"/> <xsl:template match="elements"> <root> <xsl:apply-templates/> </root> </xsl:template> <xsl:template match="element"> <xsl:variable name="valuekey2" select="key2"/> <xsl:variable name="namekey2" select="key2/name()"/> <xsl:if test="key[text() = '2'] , $valuekey2 != ''"> <newelement> <!--use key2 attribute of <newelement>--> <xsl:attribute name="{$namekey2}"><xsl:value-of select="$valuekey2"/></xsl:attribute> <!--use key2 within <newelemnt>--> <xsl:value-of select="$valuekey2"/> </newelement> </xsl:if> </xsl:template> </xsl:stylesheet>

xslt xpath condition xslt-2.0

Comments