1

This code is not giving desired result.Please help. This code is not giving desired result.Please help. This code is not giving desired result.Please help.

 <xsl:template match="/">
<xsl:variable name="Resp">
    <Response>
        <Status>
            <A></A>
        </Status>
        <RespRec>
            <Data>
                <A1>gmailcom</A1>
                <B1>YES</B1>
                <C1>PRIVACY</C1>
                <D1>00</D1>
            </Data>
            <Data>
                <A1>Ymailcom</A1>
                <B1>DES</B1>
                <C1>TYPE</C1>
                <D1>01</D1>
            </Data>
        </RespRec>
    </Response>
</xsl:variable>
<xsl:element name="Response">

    <xsl:for-each select="$Resp/Response/RespRec/Data">
        <ABCD> <!-- It can repeat as many as Data element is there -->
            <A1>
                <xsl:value-of select="$Resp/Response/RespRec/Data/A1" />
            </A1>
            <B1>
                <xsl:value-of select="$Resp/Response/RespRec/Data/B1" />
            </B1>
            <C1>
                <xsl:value-of select="$Resp/Response/RespRec/Data/C1" />
            </C1>
            <D1>
                <xsl:value-of select="$Resp/Response/RespRec/Data/D1" />
            </D1>
        </ABCD>
    </xsl:for-each>

</xsl:element>
 </xsl:template>

Desired out put:

 <Response>
  <ABCD>
  <A1>gmailcom</A1>
                        <B1>YES</B1>
                        <C1>PRIVACY</C1>
                        <D1>00</D1>
</ABCD>
<ABCD>
<A1>Ymailcom</A1>
                        <B1>DES</B1>
                        <C1>TYPE</C1>
                        <D1>01</D1>
  </ABCD>
  </Response>

But the above code is not giving desired result.Please help. But the above code is not giving desired result.Please help. But the above code is not giving desired result.Please help.

2 Answers 2

1

Assuming you use an XSLT 2.0 processor you can access the contents of a variable with XPath but you need to use relative expressions inside of your for-each e.g.

<xsl:for-each select="$Resp/Response/RespRec/Data">
        <ABCD> <!-- It can repeat as many as Data element is there -->
            <A1>
                <xsl:value-of select="A1" />
            </A1>

I would however suggest to write a template and to push nodes to the template with apply-templates.

Sign up to request clarification or add additional context in comments.

Comments

0

In XSLT 1.0, the XML you output like that is what's called a result tree fragment. It's not a node set, so you can't use it in an XPath selection. Basically the only thing you can do with it is copy it to the result document. (XSLT 2.0 fixed this silliness, thankfully.)

However, if your XSLT processor supports the EXSLT extensions, you can use exsl:node-set to convert the result tree fragment to a node set. http://exslt.org/exsl/functions/node-set/index.html

<xsl:for-each select="exsl:node-set($Resp)/Response/RespRec/Data">

Make sure to declare the exsl namespace prefix somewhere, preferably on the xsl:stylesheet element.

xmlns:exsl="http://exslt.org/common"

Also, your xsl:value-of statements don't do what you want them to do. When you're inside the xsl:for-each loop, the context node is a Data node. You're then trying to select again from the top, rather than just selecting the child element. Change them to just look like this:

<A1>
  <xsl:value-of select="A1" />
</A1>

1 Comment

How to select child element having same names. Example: <Data><Tag1></Tag1><Tag1></Tag1></Data>. And this nodeset is also stored in a variable.

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.