0
votes

I am looking for XSLT(1.0) code for below input and output XML.

In output XML, there can be any child node under C6 element. In below XML, i have put CN element but it could be any name.

Input XML -

    <?xml version = "1.0" encoding = "UTF-8"?>
    <root>
        <input>
            <c2>
                <c3>
                    <c4>c4</c4>
                </c3>
            </c2>
        </input>
        <output>
            <c5>
                <c6>
                    <CN>
                        <T1></T1>
                        <T2></T2>
                    </CN>
                </c6>
                <c6>
                    <CN>
                        <T1></T1>
                        <T2></T2>
                    </CN>
                </c6>
            </c5>
        </output>
    </root>

Desired Output XML-

    <root>
    <output>
            <c5>
                <c6>
                <!-- It could have any child node. Putting an example with CN child node name.-->
                    <CN>
                        <T1></T1>
                        <T2></T2>
                        <c3>
                            <c4>c4</c4>
                            <NewNode>current number of CN node which will be 1</NewNode>
                            <NewNode1>total number of C6 nodes which will be 2.</NewNode1>
                        </c3>
                    </CN>
                </c6>
                <c6>
                    <CN>
                        <T1></T1>
                        <T2></T2>
                        <c3>
                            <c4>c4</c4>
                            <NewNode>current number of CN node which will be 2</NewNode>
                            <NewNode1>total number of C6 nodes which will be 2.</NewNode1>
                        </c3>
                    </CN>
                </c6>
            </c5>
        </output>
    </root>

Thank you in advance.

1
You've given us the requirements, but you haven't shown us what you've tried or where you're having problems. There's no actual question. It seems more like a "Do you haz teh codez?" request.Daniel Haley
While wurken on da codez, maybe you could explain why the the first NewNode has a 2 and the second NewNode has a 1. Also, why the NewNode1 get the values it does.Bluewood66
@DanielHaley - Thank you for your comment. Actually I am not expert in XSLT. Since it seems bit complex to me, i posted my requirement.Nilay
@Bluewood66 - Thank you for your comment. I have edited my question. Please let me know if any other concern.Nilay
@Nilay, XSLT is rather complex, which is an excellent reason to show some respect to the people here who can answer your question by demonstrating that you've made a fair go at trying to solve the problem yourself, and thereby also giving them something to start with. If instead you just want people to write code to your spec, then I'm sure you can hire someone to do so.John Bollinger

1 Answers

0
votes

Use the following stylesheet:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />

  <xsl:template match="c6/*">
    <xsl:copy>
        <xsl:variable name="v1" select="count(../preceding-sibling::*)+1"/>
        <xsl:variable name="v2" select="count(../../*)"/>
        <xsl:apply-templates/>
        <xsl:apply-templates select="../../../../input/c2/c3">
          <xsl:with-param name="v1" select="$v1"/>
          <xsl:with-param name="v2" select="$v2"/>
        </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="c3">
    <xsl:param name="v1"/>
    <xsl:param name="v2"/>
    <xsl:copy>
      <xsl:apply-templates/>
      <NewNode><xsl:value-of select="$v1"/></NewNode>
      <NewNode1><xsl:value-of select="$v2"/></NewNode1>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="input"/>

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>
</xsl:stylesheet>