Re: [xsl] ID Generation for XML Element

Subject: Re: [xsl] ID Generation for XML Element
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 29 Oct 2010 13:04:03 +0100
Change the xsl:number call to use level="any".

Michael Kay
Saxonica

On 29/10/2010 12:45, Ramkumar.V wrote:
Hi, Martin

Below are the inputs. I need to generate sequential ID for all the sect element including any nesting element sec/sec/sec for sec2 sec 3 sec4

XML
====
<?xml version="1.0" encoding="UTF-8"?>
<body>
<sect>
<title>Section Head1</title>
<para>Paragraph text</para>
<para>Element para or some other any element and any no.of element may come here</para>
<sect>
<title>Section Head2</title>
<para>Paragraph text</para>
</sect>
</sect>
<para>Element para or some other any element and any no.of element may come here</para>
<sect>
<title>Section Head3</title>
<para>Paragraph text</para>
</sect>
</body>


XSLT
=====
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
<xsl:output method="xml" encoding="us-ascii" indent="no" omit-xml-declaration="no" include-content-type="no" />
<xsl:template match="body">
<article><xsl:apply-templates/></article>
</xsl:template>
<xsl:template match="sect">
<xsl:variable name="pos"><xsl:number/></xsl:variable>
<xsl:choose>
<xsl:when test="ancestor-or-self::body/sect"><sec1 id="sec{$pos}"><xsl:apply-templates/></sec1></xsl:when>
<xsl:when test="ancestor-or-self::body/sect/sect"><sec2 id="sec{$pos}"><xsl:apply-templates/></sec2></xsl:when>
<xsl:when test="ancestor-or-self::body/sect/sect/sect"><sec3 id="sec{$pos}"><xsl:apply-templates/></sec3></xsl:when>
<xsl:when test="ancestor-or-self::body/sect/sect/sect/sect"><sec4 id="sec{$pos}"><xsl:apply-templates/></sec4></xsl:when>
</xsl:choose> </xsl:template>
<xsl:template match="sect/title"><title><xsl:apply-templates/></title></xsl:template>
<xsl:template match="para"><p><xsl:apply-templates/></p></xsl:template> </xsl:stylesheet>


Current Output
===========
<?xml version="1.0" encoding="us-ascii"?>
<sec1 id="sec1">
<title>Section Head1</title>
<p>Paragraph text</p>
<p>Element para or some other any element and any no.of element may come here</p>
<sec1 id="sec1">
<title>Section Head2</title>
<p>Paragraph text</p>
</sec1>
</sec1>
<p>Element para or some other any element and any no.of element may come here</p>
<sec1 id="sec2">
<title>Section Head3</title>
<p>Paragraph text</p>
</sec1>


Expected:
=======
<?xml version="1.0" encoding="us-ascii"?>
<sec1 id="sec1">
<title>Section Head1</title>
<p>Paragraph text</p>
<p>Element para or some other any element and any no.of element may come here</p>
<sec2 id="sec2">
<title>Section Head2</title>
<p>Paragraph text</p>
</sec2>
</sec1>
<p>Element para or some other any element and any no.of element may come here</p>
<sec1 id="sec3">
<title>Section Head3</title>
<p>Paragraph text</p>
</sec1>


Regards
Ramkumar


=========================================================== ***Disclaimer***


This email, and any attachments ("this email"), is confidential. If you are not the addressee please tell the sender immediately, and destroy this email without using, sending or storing it. Any opinions, express or implied, in this email, are those of the sender, and are not necessarily approved by the company. Except as expressly stated, this e-mail should not be regarded as an offer, solicitation, recommendation or agreement to buy or sell products or services, or to enter into any contract. E-mail transmissions are not secure and may suffer errors, viruses, delay, interception and amendment. The company does not accept liability for damage caused by any of the foregoing.


ALL INCOMING AND OUTGOING MAILS MAY BE MONITORED BY THE COMPANY.
============================================================

Current Thread