xsl-list
[Top] [All Lists]

[xsl] ID Generation for XML Element

2010-10-29 05:29:52
Hi, List

I need to generate ID sequence for the below element 'sect'. How we can do? could you please suggest anyone?

Input:
====
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <sect>
       <title>Section Head1</title>
       <para>Paragraph text</para>
   </sect>
<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>
<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>
</root>

XSLT tried
========
<?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="root"><article><xsl:apply-templates/></article></xsl:template> <xsl:template match="sect"><section id="sec{position() div 2}"><xsl:apply-templates/></section></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"?><article>
   <section id="sec1">
       <title>Section Head1</title>
       <p>Paragraph text</p>
   </section>
<p>Element para or some other any element and any no.of element may come here</p>
   <section id="sec3">
       <title>Section Head2</title>
       <p>Paragraph text</p>
   </section>
<p>Element para or some other any element and any no.of element may come here</p>
   <section id="sec5">
       <title>Section Head3</title>
       <p>Paragraph text</p>
   </section>
</article>
Expected Output
============
<?xml version="1.0" encoding="us-ascii"?><article>
   <section id="sec1">
       <title>Section Head1</title>
       <p>Paragraph text</p>
   </section>
<p>Element para or some other any element and any no.of element may come here</p>
   <section id="sec2">
       <title>Section Head2</title>
       <p>Paragraph text</p>
   </section>
<p>Element para or some other any element and any no.of element may come here</p>
   <section id="sec3">
       <title>Section Head3</title>
       <p>Paragraph text</p>
   </section>
</article>

Regards,

Ram Kumar V.



===========================================================
***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.
============================================================

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--