Jindřich Mynarz
Jindřich Mynarz

Reputation: 1613

Failing XPath on xsl:variable with XML fragment

I must be missing something very basic. I want to look up a key from a transformed XML document in XML fragment stored in xsl:variable. Here's a minimal example:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<code>A</code>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:variable name="mappings">
        <mapping key="A">Amy</mapping>
    </xsl:variable>

    <xsl:template match="code">
        <xsl:value-of select="$mappings/mapping[@key = text()]"/> 
    </xsl:template>
</xsl:stylesheet>

Transforming this XML document with the XSL stylesheet produces empty result. It seems that the comparison @key = text() is wrong, because when I use <xsl:value-of select="$mappings/mapping[@key = 'A']"/>, it retrieves the expected value (i.e. "Amy"). What am I missing?

Upvotes: 3

Views: 571

Answers (2)

potame
potame

Reputation: 7905

With an intermediate variable, it works properly :

<xsl:template match="code">
    <xsl:variable name="keyval" select="./text()" />
    <out>
       <xsl:value-of select="$mappings/mapping[@key = $keyval]"/>
    </out>
</xsl:template>

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167716

Use

<xsl:template match="code">
    <xsl:value-of select="$mappings/mapping[@key = current()]"/> 
</xsl:template>

Upvotes: 3

Related Questions