Level information for XML

gsoftwares

Usenet Archive
Joined
Dec 7, 2004
Messages
6
Reaction score
0
HI Gurus,

I have xml file in this format

<hierarchy>
<entity>
<contents>
<entity>
</entity>
</contents>
</entity>
<entity>
<contents>
<entity>
</entity>
</contents>
</entity>
</hierarchy>

I want to have attribute level for all <entity> node
like

<entity level=1>
<contents>
<entity level=2>
</entity>
</contents>
</entity>
<entity level=1>
<contents>
<entity level=2>
</entity>
</contents>
</entity>

Can someone help to get this using xslt.

thanks
gbk
 

Joris Gillis

Usenet Archive
Joined
Sep 28, 2004
Messages
355
Reaction score
0
HI Gurus,
I have xml file in this format

<hierarchy>
<entity>
<contents>
<entity>
</entity>
</contents>
</entity>
<entity>
<contents>
<entity>
</entity>
</contents>
</entity>
</hierarchy>

I want to have attribute level for all <entity> node

Can someone help to get this using xslt.
Coming right up:)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:eek:utput indent="yes" method="xml"/>

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

<xsl:template match="entity">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="level"><xsl:value-of select="count(ancestor::entity) +1 "/></xsl:attribute> <xsl:apply-templates/> </xsl:copy> </xsl:template>

<xsl:template match="contents">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

P.S. remember that attributes have to be qouted at all times in XML.

regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top