[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: ID Generation for XML Element

Subject: Re: ID Generation for XML Element
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 29 Oct 2010 13:04:03 +0100
Re:  ID Generation for XML Element
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.