user3872094
user3872094

Reputation: 3351

Auto increment inside template

I've the below XML.

<?xml version="1.0" encoding="UTF-8"?>
<uaeccc>
    <nd>
    <AN>-2-0001</AN>
        <h2>Ttle2</h2>
    </nd>
    <nd>
        <h1>Ttle1</h1>
        <h2>Ttle2</h2>
        <h3>Part 1 Contracts</h3>
        <h4>par2 sub contractts</h4>
    </nd>
    <nd>
        <h1>Ttle1</h1>
        <h2>Ttle2 </h2>
        <h3>Part 1 Contracts</h3>
        <h4>Part 2 Sub contracts</h4>
    </nd>
    <nd>
        <h1>Ttle1</h1>
        <h2>Ttle2 </h2>
        <h3>Part 1 Contracts</h3>
        <h4>part 2 sub contracts</h4>
    </nd>
</uaeccc>

Here i was trying to increment a number, but it is getting incremented only in that section.

Below is my XSLT.

<xsl:template match="uaeccc">
    <xsl:for-each select="nd">
        <xsl:apply-templates select="."/>
    </xsl:for-each>
</xsl:template>

<xsl:template name="section" match="nd">
    <xsl:apply-templates select="./node()[1][self::page]" mode="first"/>
    <!-- Variables-->
    <xsl:variable name="getChap">
        <xsl:value-of select="substring-before(substring-after((//AN)[1],'-'),'-')"/>
    </xsl:variable>
    <xsl:variable name="count">
        <xsl:number level="any"/>
    </xsl:variable>
    <xsl:variable name="classname">
        <!--Get name attribute of current node -->
        <xsl:value-of select="concat('section-sect','1')"/>
    </xsl:variable>

    <xsl:variable name="Chn">
        <xsl:value-of select="format-number($getChap,'00')"/>
    </xsl:variable>
    <xsl:variable name="chapternumber">
        <xsl:value-of select="$Chn"/>
    </xsl:variable>
    <xsl:variable name="sectnum">
        <xsl:number level="any" count="section" format="1"/>
    </xsl:variable>
    <!--Create a string variable by concat string method  -->
    <xsl:variable name="sectionname">
        <xsl:value-of select="concat('CH_',$chapternumber,'-SEC-', $count)"/>
    </xsl:variable>
    <!-- Template Content  -->
    <div class="{$classname}">
        <a name="{$sectionname}"> </a>
        <div class="section-title">
            <xsl:apply-templates select="h3"/>
        </div>
        <xsl:if test="./h4">
            <div class="{$classname}">
                <a name="{$sectionname}"> </a>
                <div class="section-title2">
                    <xsl:apply-templates select="h4"/>
                </div>
            </div>
        </xsl:if>
        <xsl:apply-templates select="child::node()[not(self::h4| self::h3)]"/>
    </div>
</xsl:template>

<xsl:template match="AN"/>

<xsl:template match="h1"/>

<xsl:template match="h2"/>

My current output is

  <div class="section-sect1">
        <a name="CH_02-SEC-1"></a>
        <div class="section-title"></div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-2"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-2"></a>
            <div class="section-title2">
                <h4>par2 sub contractts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-3"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-3"></a>
            <div class="section-title2">
                <h4>Part 2 Sub contracts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-4"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-4"></a>
            <div class="section-title2">
                <h4>part 2 sub contracts</h4>
            </div>
        </div>
    </div>

and expected output is

 <div class="section-sect1">
        <a name="CH_02-SEC-1"></a>
        <div class="section-title"></div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-2"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-3"></a>
            <div class="section-title2">
                <h4>par2 sub contractts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-4"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-5"></a>
            <div class="section-title2">
                <h4>Part 2 Sub contracts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-6"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-7"></a>
            <div class="section-title2">
                <h4>part 2 sub contracts</h4>
            </div>
        </div>
    </div>

Here i want to increment the number after SEC- every time there is $classnamecalled.But here in my XSLT, the increment occurs but is same in case of subsection also.

please let me know how can i get it fixed.

Here is a Working Demo

Upvotes: 1

Views: 141

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

I can't make heads or tails of your code. FWIW, the following stylesheet, when applied to your example input, will produce the required output (with the exception of wrapping the result in a single root element):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/uaeccc">
    <root>
        <xsl:apply-templates/>
    </root>
</xsl:template>

<xsl:template match="nd">
    <div class="section-sect1">
        <xsl:variable name="sectionnum">
            <xsl:number count="nd|h4" level="any"/>
        </xsl:variable>
        <a name="CH_02-SEC-{$sectionnum}"/>
        <div class="section-title">
            <xsl:copy-of select="h3"/>
        </div>
        <xsl:apply-templates select="h4"/>
    </div>
</xsl:template>

<xsl:template match="h4">
    <div class="section-sect1">
        <xsl:variable name="sectionnum">
            <xsl:number count="nd|h4" level="any"/>
        </xsl:variable>
        <a name="CH_02-SEC-{$sectionnum}"/>
        <div class="section-title2">
            <xsl:copy-of select="."/>
        </div>
    </div>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions