otto.kranz
otto.kranz

Reputation: 91

Use Variable for Attribute in XPath

I want to use the variable $Identifier as an attribute value in a XPath to look up some Information depending on the current $Identifier.

    <xsl:when test="self::ATTRIBUTE-VALUE-XHTML">
        <xsl:variable name="Identifier" select="./DEFINITION/ATTRIBUTE-DEFINITION-STRING-REF"/>
        <xsl:variable name="LongName" select="concat('/SPEC-OBJECT-TYPE/SPEC-ATTRIBUTES/node()[@IDENTIFIER=', $Identifier, ']/@LONG-NAME')"/>
        <xsl:element name="{$LongName}">Test</xsl:element>
    </xsl:when>

I tried to use concat() to combine the path and variable $Identifier:

<xsl:variable name="LongName" select="concat('/SPEC-OBJECT-TYPE/SPEC-ATTRIBUTES/node()[@IDENTIFIER=', $Identifier, ']/@LONG-NAME')"/>

But that didn't work. How can I get the variable to work in the Path?

Upvotes: 0

Views: 628

Answers (1)

Tim C
Tim C

Reputation: 70648

Just do this....

<xsl:variable name="LongName" select="/SPEC-OBJECT-TYPE/SPEC-ATTRIBUTES/node()[@IDENTIFIER=$Identifier]/@LONG-NAME')"/>

(Your current variable is actually just a string, and you would need to use some form of dynamic evaluation for that to work).

As an aside, you could also use a key here...

<xsl:key name="test" match="/SPEC-OBJECT-TYPE/SPEC-ATTRIBUTES/node()" use="@IDENTIFIER" />

Then your LongName variable becomes this...

<xsl:variable name="LongName" select="key('test', $Identifier)"/>

Upvotes: 1

Related Questions