Ville Myrskyneva
Ville Myrskyneva

Reputation: 1640

Having multiple choices, but need single xsl:value-of when a certain condition is met

I have an xml file which have multiple similar elements with similar child-elements. E.g.

<root>
    <elem>
        <condition>A</condition>
        <value>111</value>
    </elem>
    <elem>
       <condition>B</condition>
        <value>555</value>
    </elem>
    <elem>
        <condition>A</condition>
        <value>222</value>
    </elem>
</root>

One of these elements contain a value I need to extract in xsl. The value must be identified by a condition stated in another element in the element child nodes. Lets say the condition is 'B' and I need the value of that element '555'.

I know how to get the right value in the xml editor XPath I use (Rinzo - Eclipse) =>

/root/elem[condition='B']/value

But I fail to get (any) value when I try to get it in xsl =>

<xsl:value-of select="/root/elem[condition='B']/value"/>

The value in the xsl will be in a single element, so can not use for-each loop. Imho the original xml is badly formed, but I can not help with that.

So the question is, how do I get that value in an xsl file, using xsl:value-of?

Upvotes: 0

Views: 175

Answers (1)

Jayson Lorenzen
Jayson Lorenzen

Reputation: 575

We cannot see the rest of your XML or XSL but your value-of statement works as shown.

<xsl:template match="/">
  <xsl:value-of select="/root/elem[condition='B']/value"/>
</xsl:template>

That works and returns: 555

As Flynn1179 points out, in the comments, your XML probably contains a default namespace and that is not being take into account in the XPATH.

Try adding the namespace to the XPATH either inline (replace http://yournamespace-here.example.com with the actual URL):

/*[namespace-uri()='http://yournamespace-here.example.com' and local-name()='root']/*[namespace-uri()='http://yournamespace-here.example.com' and local-name()='elem'][condition='B']/*[namespace-uri()='http://yournamespace-here.example.com' and local-name()='value']

or by giving it a prefix in your XSL

xmlns:myPrefix="http://yournamespace-here.example.com" 

and then adding that to your XPATH

/myPrefix:root/myPrefix:elem[condition='B']/myPrefix:value

Upvotes: 1

Related Questions