Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto increment seq num

Status
Not open for further replies.

agsp123

Programmer
Aug 5, 2002
1
US
Depending on the distinct value of "type" in the following xml I need to generate a sequence number and add that to the resulting xml.

For. If there are two distinct "type" elements then
"sequence number" should be 1 for the first one
and 2 for the second type.
<Sample>
<num>5700</num>
<MAIN>
<type>1101</type>
<SUB>
        <text>aaa</text>
</SUB>
</MAIN>
<MAIN>
<type>1102</type>

<SUB>
    <text>bbbb</text>
</SUB>
</MAIN>
<MAIN>
<trans_type>1101</trans_type>
<SUB>
    <text>cccc</text>
</SUB>
</MAIN>
</Sample>


Result should be as follows:
<Sample>
<num>5700</num>
<MAIN>
<type>1101</type>
<sequencenum>1</<sequencenum>
<SUB>
        <text>aaa</text>
</SUB>
</MAIN>
<MAIN>
<type>1102</type>
<sequencenum>2</<sequencenum>
<SUB>
    <text>bbbb</text>
</SUB>
</MAIN>
<MAIN>
<trans_type>1101</trans_type>
<sequencenum>1</<sequencenum>
<SUB>
    <text>cccc</text>
</SUB>
</MAIN>
</Sample>

Please let me know how to get the result in xslt.
I tried using count and number but did not got the answer.
Any help is highly appreciated.

Regards,
agsp
 
use the xsl file below:

<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;<xsl:eek:utput method=&quot;xml&quot; indent=&quot;yes&quot;/>

<xsl:template match=&quot;/&quot;>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match=&quot;Sample&quot;>
<Sample>
<xsl:apply-templates/>
</Sample>
</xsl:template>

<xsl:template match=&quot;MAIN&quot;>
<xsl:variable name=&quot;trans&quot; select=&quot;type&quot;/>
<MAIN>
<xsl:apply-templates select=&quot;type&quot;/>
<xsl:call-template name=&quot;sequencenum&quot;>
<xsl:with-param name=&quot;counter&quot;>
<xsl:value-of select=&quot;count(/Sample/MAIN[type=$trans])&quot;/>
</xsl:with-param>
</xsl:call-template>
<xsl:apply-templates select=&quot;SUB&quot;/>
</MAIN>
</xsl:template>
<xsl:template match=&quot;num&quot;>
<num>
<xsl:value-of select=&quot;.&quot;/>
</num>
</xsl:template>
<xsl:template match=&quot;text&quot;>
<text>
<xsl:value-of select=&quot;.&quot;/>
</text>
</xsl:template>
<xsl:template match=&quot;type&quot;>
<type>
<xsl:value-of select=&quot;.&quot;/>
</type>
</xsl:template>
<xsl:template match=&quot;SUB&quot;>
<SUB>
<xsl:apply-templates select=&quot;text&quot;/>
</SUB>
</xsl:template>
<xsl:template name=&quot;sequencenum&quot;>
<xsl:param name=&quot;counter&quot;/>
<sequencenum>
<xsl:value-of select=&quot;$counter&quot;/>
</sequencenum>
</xsl:template>
</xsl:stylesheet>

______________________________________________________
if you have any questions let me konw
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top