looping through names of nodes and sub nodes

Discussion in 'XML' started by k.a.bouton@reading.ac.uk, Sep 20, 2005.

  1. Guest

    I am trying to transform my XML to produce just a tree of the unique
    nodes and subnodes and am having no luck.

    this is the sample xml

    <MyRoot>
    <contact>
    <name>Jane Doe</name>
    <country>USA</country>
    </contact>
    <magazines>
    <magazine>
    <magazine_name>Some Magazine</magazine_name>
    <year>2004</year>
    </magazine>
    <magazine>
    <magazine_name>Some Magazine</magazine_name>
    <year>2004</year>
    </magazine>
    </magazines>
    </MyRoot>

    Desired Text Result from transformation
    Group: MyRoot
    Group:contact
    name
    country
    End_Group:contact
    Group:magazines
    Group: magazine
    magazine_name
    year
    End_Group:magazines
    End_Group:MyRoot

    I am getting lost in the looping of it as far as starting and ending
    the group notation.

    Pointers and suggestions welcome
    K
     
    , Sep 20, 2005
    #1
    1. Advertising

  2. Joris Gillis Guest

    Hi,

    Tempore 15:48:16, die Tuesday 20 September 2005 AD, hinc in foro {comp.text.xml} scripsit <>:

    > <MyRoot>
    > <contact>
    > <name>Jane Doe</name>
    > <country>USA</country>
    > </contact>
    > <magazines>
    > <magazine>
    > <magazine_name>Some Magazine</magazine_name>
    > <year>2004</year>
    > </magazine>
    > <magazine>
    > <magazine_name>Some Magazine</magazine_name>
    > <year>2004</year>
    > </magazine>
    > </magazines>
    > </MyRoot>
    >
    > Desired Text Result from transformation
    > Group: MyRoot
    > Group:contact
    > name
    > country
    > End_Group:contact
    > Group:magazines
    > Group: magazine
    > magazine_name
    > year
    > End_Group:magazines
    > End_Group:MyRoot


    Try:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text"/>

    <xsl:key name="element" match="*" use="name()"/>

    <xsl:template match="*">
    <xsl:if test="generate-id()=generate-id(key('element',name())[1])">
    <xsl:for-each select="ancestor::*">
    <xsl:text> </xsl:text>
    </xsl:for-each>
    <xsl:if test="not(*)">
    <xsl:value-of select="name()"/>
    <xsl:text>
    </xsl:text>
    </xsl:if>
    <xsl:if test="*">
    <xsl:text/>Group: <xsl:value-of select="name()"/>
    <xsl:text>
    </xsl:text>
    <xsl:apply-templates select="*"/>
    <xsl:for-each select="ancestor::*">
    <xsl:text> </xsl:text>
    </xsl:for-each>
    <xsl:text/>End_Group: <xsl:value-of select="name()"/>
    <xsl:text>
    </xsl:text>
    </xsl:if>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    This might become rather tough when the real structure contains identically named nodes in another context.

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Don't send spam. I don't like it and it is illegal.
     
    Joris Gillis, Sep 20, 2005
    #2
    1. Advertising

  3. bouton Guest

    that seems to work prefect.Thanks. it was the generate_id where I got
    lost. thanks again
    K
     
    bouton, Sep 20, 2005
    #3
  4. bouton Guest

    OK - now I'd like to get fancy...
    Is there anyway, if there are nodes which are repeated to indicate it?
    eg
    if if node names has multiple name under it
    <magazines>
    <magazine>some mag</magazine>
    <magazine>another mag</magazine>
    </magazines>

    to print out
    + Group: magazines
    Thanks
    K
     
    bouton, Sep 22, 2005
    #4
  5. Joris Gillis Guest

    Hi,

    Tempore 17:37:44, die Thursday 22 September 2005 AD, hinc in foro {comp.text.xml} scripsit bouton <>:

    > Is there anyway, if there are nodes which are repeated to indicate it?
    > eg
    > if if node names has multiple name under it
    > <magazines>
    > <magazine>some mag</magazine>
    > <magazine>another mag</magazine>
    > </magazines>


    Try adding:
    <xsl:if test="count(../*[name()=name(current())]) &gt; 1">+</xsl:if>

    like in this stylesheet:
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text"/>

    <xsl:key name="element" match="*" use="name()"/>

    <xsl:template match="*">
    <xsl:if test="generate-id()=generate-id(key('element',name())[1])">
    <xsl:variable name="indent">
    <xsl:for-each select="ancestor::*">
    <xsl:text> </xsl:text>
    </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select="$indent"/>
    <xsl:if test="count(../*[name()=name(current())]) &gt; 1">+</xsl:if>
    <xsl:if test="not(*)">
    <xsl:value-of select="name()"/>
    <xsl:text>
    </xsl:text>
    </xsl:if>
    <xsl:if test="*">
    <xsl:text/>Group: <xsl:value-of select="name()"/>
    <xsl:text>
    </xsl:text>
    <xsl:apply-templates select="*"/>
    <xsl:value-of select="$indent"/>
    <xsl:text/>End_Group: <xsl:value-of select="name()"/>
    <xsl:text>
    </xsl:text>
    </xsl:if>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    «Error, keyboard not found— press F1 to continue» , BIOS
     
    Joris Gillis, Sep 22, 2005
    #5
  6. bouton Guest

    perfect- thanks
    K
     
    bouton, Sep 22, 2005
    #6
    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. =?Utf-8?B?RGVlcA==?=

    RE: Looping through XML Nodes

    =?Utf-8?B?RGVlcA==?=, Jul 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    411
    =?Utf-8?B?RGVlcA==?=
    Jul 9, 2004
  2. Ben
    Replies:
    2
    Views:
    936
  3. johkar
    Replies:
    2
    Views:
    2,168
    johkar
    Apr 2, 2009
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,128
    Lawrence D'Oliveiro
    May 20, 2011
  5. Ralph Hartman
    Replies:
    1
    Views:
    216
    Raghavendra T V
    Aug 25, 2004
Loading...

Share This Page