user2421350
user2421350

Reputation: 157

how to get the value of a node in xsl based on a value from other node

How do I get the the index of the string that is provided inside node "s" and find the corresponding index value based on the name in the "strings" node?

This is my XSL template:

 <xsl:template match="norm" >
     <xsl:variable name="name" select="@string" />
     <xsl:value-of select="/s/string/@name=$name/@index"/>,
 </xsl:template>

This is a fragment from my source XML:

<s name="main">
    <norm string="back-slash"/>
    <norm string="open-braces" />
    <norm string="close-braces" />
</s>
<strings  name="consts">
  <string name="back-slash" val="\\" index="0"/>
  <string name="close-braces" val="]" index="2"/>
  <string name="remove-null" val="null" index="3" /> 
</strings>

Upvotes: 0

Views: 58

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167401

I would define a key <xsl:key name="s" match="strings/string" use="@name"/> and then use <xsl:value-of select="key('s', @string)/@index"/> in the template.

Upvotes: 1

Colin D
Colin D

Reputation: 5661

You want:

<xsl:value-of select="//strings/string[@name=$name]/@index" />

The big thing you did wrong was you used / instead of [ and ] to enclose your conditional @name=$name.

You also used /s when you want //strings. There is no s/string element.

Upvotes: 1

Related Questions