how do i choose between a same name element with different attributes?

Perhaps you could do something simpler:

XML

<parent>
    <item>
        <name>Col</name>
        <name lang="en">Red</name>
    </item>
    <item>
        <name>Sha</name>
        <name lang="en">Square</name>
    </item>
</parent>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<xsl:template match="parent">
    <xsl:text>Color: </xsl:text>
    <xsl:value-of select="item[name='Col']/name[@lang='en']"/>
    <xsl:text>&#10;Shape: </xsl:text>
    <xsl:value-of select="item[name='Sha']/name[@lang='en']"/>
</xsl:template>

</xsl:stylesheet>

Result

Color: Red
Shape: Square

Note that the values in my XML example contain no quotes.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top