Reputation: 1
How can I output value of one element which depends on anothers element value ? I want to use another element's value in place of XXX
<xsl:value-of select="/node1/node2[@name=XXX]"/@lng>
and in XXX place I want value of:
/node3/node4/@val
simplified xml:
<node>
<node1>
<node2 name="abc" lng="uk">
</node2>
</node1>
<node3>
<node4 val="abc">
</node4>
</node3>
</node>
I know it can be achieved by:
<xsl:value-of select="/node1/node2[@name='abc']"/@lng>
but I don't know what value will be in node4/@val How can I achieve it?
Upvotes: 0
Views: 1552
Reputation: 70618
One answer to your question would be to do this...
<xsl:value-of select="/node/node1/node2[@name=/node/node3/node4/@val]/@lng" />
Or slightly better, to use a variable
<xsl:variable name="node4val" select="/node/node3/node4/@val" />
<xsl:value-of select="/node/node1/node2[@name=$node4val]/@lng" />
But I am guessing you may have multiple node4
elements in your XML, and perhaps you want to get the value for each one? In this case, it would look more like this...
<xsl:for-each select="/node/node3/node4">
<xsl:value-of select="/node/node1/node2[@name=current()/@val]/@lng" />
</xsl:for-each>
Or, with a variable
<xsl:for-each select="/node/node3/node4">
<xsl:variable name="val" select="@val" />
<xsl:value-of select="/node/node1/node2[@name=$val]/@lng" />
</xsl:for-each>
Better still would be to use a key. Try this, for example
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="node2s" match="node2" use="@name" />
<xsl:template match="/">
<xsl:for-each select="/node/node3/node4">
<xsl:value-of select="key('node2s', @val)/@lng" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1