Level information for XML

Discussion in 'XML' started by gsoftwares@gmail.com, Dec 7, 2004.

  1. Guest

    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
    , Dec 7, 2004
    #1
    1. Advertising

  2. Joris Gillis Guest

    > 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,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
    Joris Gillis, Dec 7, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    438
  2. =?Utf-8?B?Sm9l?=

    page-level vs control-level enableViewState

    =?Utf-8?B?Sm9l?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,926
    S. Justin Gengo
    Oct 26, 2005
  3. Replies:
    8
    Views:
    459
  4. Replies:
    1
    Views:
    371
    Joris Gillis
    Dec 7, 2004
  5. pabbu
    Replies:
    8
    Views:
    719
    Marc Boyer
    Nov 7, 2005
Loading...

Share This Page