Mark Salamon
Mark Salamon

Reputation: 579

Matching Attribute value to key in XSL

I have a key defined in my XSL file, where you pass in a string equal to the value of node "d" to return parent node "c":

<xsl:key name="column" match="/a/b/c" use="d"/>

Each node "c" has a unique @position attribute.

I need to use the key function for this to match the @position attribute of another node, "x", which @position value will match the value of the @position attribute in the "c" node, like:

<xsl:value-of select="/a/x[@position='##']/y/z"/>

I want to replace ## in the attribute matching statement with the equivalent of:

<xsl:value-of select="key('column','foo')/@position"/>

I have tried to use the expression shorthand in curly braces, like:

<xsl:value-of select="/a/x[@position={key('column','foo')/@position}]/y/z"/>

but that did not work. I could define a variable that is equal to the key value:

<xsl:variable name="var1" select="key('column','foo')/@position"/>

and use that variable in the attribute equality statement:

<xsl:value-of select="/a/x[@position=$var1]/y/z"/>

but that creates an unnecessary extra step.

Upvotes: 0

Views: 243

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Don't use curly braces in select attribute expressions:

<xsl:value-of select="/a/x[@position=key('column','foo')/@position]/y/z"/>

Obviously that asks for a key match="a/x" use="@position" and simply

<xsl:value-of select="key('k2', key('column','foo')/@position)/y/z"/>

Upvotes: 1

Related Questions