Mohan
Mohan

Reputation: 107

How to get the evaluated value from a XSLT Variable

I am doing a XML transformation using XSLT. The scenario I am stuck up with is, I have many parent elements and a child element with the same name "OtherDetails" in all the parent element. So, I iterate the parent element to get the value of child element "OtherDetails". So, I try this way.

<xsl:if test".='Z'">
<xsl:variable name="parent" select="concat(name(..),'/OtherDetails')"/>
<xsl:value-of select="$parent" />
</xsl:if>

When I try a I get the concatenated string of $parent variable but I need to get the XPath value of $parent variable.

I need to get the values of OtherDetails when the element "Other" value is 'Z' Sample XML:

<structure>
<Other/>
<OtherDetails>Value1</OtherDetails>
</structure>
<power>
<Other>Z</Other>
<OtherDetails>Value2</OtherDetails>
</power>
<restrict>
<Other>Z</Other>
<OtherDetails>Value3</OtherDetails>
</restrict>

Upvotes: 0

Views: 205

Answers (1)

Tim C
Tim C

Reputation: 70618

I think what you need to do is just this...

<xsl:variable name="parent" select="../OtherDetails"/>

This assumes you are positioned on an Other node, and want to get the OtherDetails under the same parent.

Note, you can also do this, but it would only work if OtherDetails was always following Other

<xsl:variable name="parent" select="following-sibling::OtherDetails"/>

Also note variables are local in scope. You would not be able to use the $parent outside of the xsl:if in your particular example.

Upvotes: 2

Related Questions