Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > looping through names of nodes and sub nodes

Reply
Thread Tools

looping through names of nodes and sub nodes

 
 
k.a.bouton@reading.ac.uk
Guest
Posts: n/a
 
      09-20-2005
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

 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      09-20-2005
Hi,

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

> <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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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.
 
Reply With Quote
 
 
 
 
bouton
Guest
Posts: n/a
 
      09-20-2005
that seems to work prefect.Thanks. it was the generate_id where I got
lost. thanks again
K

 
Reply With Quote
 
bouton
Guest
Posts: n/a
 
      09-22-2005
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

 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      09-22-2005
Hi,

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

> 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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
 
Reply With Quote
 
bouton
Guest
Posts: n/a
 
      09-22-2005
perfect- thanks
K

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Options for looping through children with different element names johkar XML 2 04-02-2009 11:06 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
Looping through all rows and columns (to get sub-totals for each column and row) Ralph Hartman ASP .Net Datagrid Control 1 08-25-2004 12:00 PM
RE: Looping through XML Nodes =?Utf-8?B?RGVlcA==?= ASP .Net 0 07-09-2004 09:24 AM



Advertisments