Flat to hierarchical using attributes

Discussion in 'XML' started by Steve Mac, Dec 30, 2004.

  1. Steve Mac

    Steve Mac Guest

    I am very new to XML / XSL and am having trouble with transforming a
    flat xml file into a hierarchical format using an id / parentid
    attribute relationship. Any help on this would be greatly appreciated.

    I have an XML document in the following format (there are other
    extraneous attributes I've removed for simplicity):

    <?xml version="1.0" encoding="UTF-8"?>
    <sitenav>
    <navitem id="10423" displaytitle="A Child" parentid="" />
    <navitem id="10421" displaytitle="A Grandchild" parentid="10423" />
    <navitem id="10476" displaytitle="A Great Great Grandchild - parent
    is 10434" parentid="10434" />
    <navitem id="10424" displaytitle="Another Grandchild"
    parentid="10423" />
    <navitem id="10434" displaytitle="A Great Grandchild - parent is
    10421" parentid="10421" />
    <navitem id="10425" displaytitle="Another child" parentid="" />
    ...
    </sitenav>

    And need to transform it into this format:

    <?xml version="1.0" encoding="UTF-8"?>
    <sitenav>
    <navitem id="10423" displaytitle="A Child" parentid="">
    <navitem id="10421" displaytitle="A Grandchild" parentid="10423">
    <navitem id="10434" displaytitle="A Great Grandchild - parent is
    10421" parentid="10421">
    <navitem id="10476" displaytitle="A Great Great Grandchild - parent
    is 10434" parentid="10434" />
    </navitem>
    </navitem>
    <navitem id="10424" displaytitle="Another Grandchild" parentid="10423"
    />
    </navitem>
    <navitem id="10425" displaytitle="Another child" parentid="" />
    ...
    </sitenav>

    Thanks,

    Steve

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Steve Mac, Dec 30, 2004
    #1
    1. Advertising

  2. Steve Mac

    Joris Gillis Guest

    > I have an XML document in the following format (there are other
    > extraneous attributes I've removed for simplicity):

    Hi,

    Nice question...

    here's one solution:

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

    <xsl:eek:utput method="xml" indent="yes"/>
    <xsl:key name="parentid" match="navitem" use="@parentid"/>

    <xsl:template match="sitenav">
    <xsl:copy>
    <xsl:apply-templates select="navitem[@parentid='']"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="navitem">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="key('parentid',@id)"/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Scio me nihil scire" - Socrates
    Joris Gillis, Dec 30, 2004
    #2
    1. Advertising

  3. Steve Mac

    Steve Mac Guest

    >Nice question...

    >here's one solution:


    Thanks Joris!

    Would this same approach work to only copy some of the attributes, or
    would I need to try a different way?

    Steve


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Steve Mac, Dec 31, 2004
    #3
  4. Steve Mac

    Joris Gillis Guest

    > Would this same approach work to only copy some of the attributes, or
    > would I need to try a different way?

    The same approach would work.
    Just change <xsl:copy-of select="@*"/>
    into <xsl:copy-of select="@foo|@bar"/> to only copy the attributes 'foo' and 'bar'

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Gaudiam omnibus traderat W3C , nec vana fides
    Joris Gillis, Dec 31, 2004
    #4
    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. Paddy McCarthy
    Replies:
    3
    Views:
    698
    Anthony J Bybell
    Sep 24, 2004
  2. Ksenia Marasanova

    convert flat structure into hierarchical one

    Ksenia Marasanova, Sep 26, 2004, in forum: Python
    Replies:
    3
    Views:
    540
    Ksenia Marasanova
    Sep 27, 2004
  3. Shannon
    Replies:
    0
    Views:
    361
    Shannon
    Sep 11, 2007
  4. Shannon
    Replies:
    9
    Views:
    860
    Brian Drummond
    Sep 11, 2007
  5. Replies:
    2
    Views:
    932
    Joseph Kesselman
    Nov 5, 2007
Loading...

Share This Page