0

I have the following XML

<root>
  <ns:Search xmlns:ns="http://example.com/1.0/">
    <ns:AllClass>
      <ns:class1>
        <ns:node1>fhgfjh</ns:node1>
        <ns:node2>Aprtyrtyril</ns:node2>
        <ns:node3>Juklyuiyly</ns:node3>
      </ns:class1>
      <ns:class2>
        <ns:node1>dfgd</ns:node1>
        <ns:node2>trytyu</ns:node2>
        <ns:node3>sgsdfg</ns:node3>
      </ns:class2>
      .
      .
      .
      .
      .
      .
    </ns:AllClass>
  </ns:Search>
  <ns:Req xmlns:ns="http://example.com/1.0/">
    <ns:classId>class1</ns:classId>
    <ns:othertag>asdfg</ns:othertag>
    .
    .
    .
    .
    .
  </ns:Req>
</root>

and the following XSL

<xsl:template match="root">
    <xsl:variable name="class" select="/root/Req/classId" />
    <ns1:Request xmlns:ns1="http://example.com/ns1">
      <ns1:node>
        <xsl:value-of xmlns:ns="http://example.com/1.0/" xmlns:ns1="http://abc.xyz.com/1.0/" select="/root/Search/AllClass[Value=$class]/node1" />
      </ns1:node>
    </ns1:Request>
  </xsl:template>

I am getting a class name inside my XML in 'classId' node. I want to get the value of 'node1' of the class same as my value inside the 'classId' tag. I am using a variable'class' and storing the value of classId and trying to get the value of 'node1' by using the variable inside Select Xpath. But it is not working. Please help.

3
  • did you try like this /root/Search/AllClass/$class/node1 Commented Jul 15, 2016 at 10:08
  • Yes already tried that Commented Jul 15, 2016 at 10:09
  • I guess this will work /root/Search/AllClass/data($class)/node1 Commented Jul 15, 2016 at 10:21

1 Answer 1

1

You have to use namespace in XPATH expressions:

<xsl:template match="root">
    <xsl:variable name="class_tmp" xmlns:ns="http://example.com/1.0/" select="/root/ns:Req/ns:classId" />
    <xsl:variable name="class" select="concat('ns:',$class_tmp)" />
    <xsl:message><xsl:value-of select="$class"/></xsl:message>
    <ns1:Request xmlns:ns1="http://example.com/ns1">
        <ns1:node>
            <xsl:value-of xmlns:ns="http://example.com/1.0/" xmlns:ns1="http://abc.xyz.com/1.0/" select="/root/ns:Search/ns:AllClass/*[name()=$class]/ns:node1" />
        </ns1:node>
    </ns1:Request>
</xsl:template>
Sign up to request clarification or add additional context in comments.

2 Comments

Well when I am using static xpath without using the variable I am getting value
Thanks, I guess namespace was the reason it was not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.