Veerelli Abhishek
Veerelli Abhishek

Reputation: 1

Sequence Number generation between multiple tags

My Input xml is in below format

<root>
    <entity>
        <entityrecord>
            <field>
                <id>Parent</id>
                <value>P-1</value>
            </field>
            <entity>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-1</value>
                    </field>
                </entityrecord>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-2</value>
                    </field>
                </entityrecord>
            </entity>
        </entityrecord>
        <entityrecord>
            <field>
                <id>Parent</id>
                <value>P-2</value>
            </field>
            <entity>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-1</value>
                    </field>
                </entityrecord>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-2</value>
                    </field>
                </entityrecord>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-3</value>
                    </field>
                </entityrecord>
            </entity>
        </entityrecord>
        <entityrecord>
            <field>
                <id>Parent</id>
                <value>P-3</value>
            </field>
            <entity>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-1</value>
                    </field>
                </entityrecord>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-2</value>
                    </field>
                </entityrecord>
                <entityrecord>
                    <field>
                        <id>Child</id>
                        <value>C-3</value>
                    </field>
                </entityrecord>
            </entity>
        </entityrecord>
    </entity>
</root>

and my output Format should be

1^P-1
2^C-1
3^C-2
4^P-2
5^C-1
6^C-2
7^C-3
8^P-3
9^C-1
10^C-2
11^C-3

I tried Many different methods to get this done but nothing worked out.I was not able to generate the sequence number.can anyone help me out in this case

Upvotes: 0

Views: 44

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117043

Assuming the repeating 6 in the provided output is a mistake, try:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="field">
    <xsl:number count="field" level="any"/>
    <xsl:text>^</xsl:text>
    <xsl:value-of select="value"/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Applied to your example input, the result will be:

1^P-1
2^C-1
3^C-2
4^P-2
5^C-1
6^C-2
7^C-3
8^P-3
9^C-1
10^C-2
11^C-3

Upvotes: 1

Related Questions