473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

looping through names of nodes and sub nodes

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 '05 #1
5 1481
Hi,

Tempore 15:48:16, die Tuesday 20 September 2005 AD, hinc in foro {comp.text.xml} scripsit <k.********@reading.ac.uk>:
<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:output 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.
Sep 20 '05 #2
that seems to work prefect.Thanks. it was the generate_id where I got
lost. thanks again
K

Sep 20 '05 #3
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

Sep 22 '05 #4
Hi,

Tempore 17:37:44, die Thursday 22 September 2005 AD, hinc in foro {comp.text.xml} scripsit bouton <k.********@reading.ac.uk>:
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:output 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
Sep 22 '05 #5
perfect- thanks
K

Sep 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark | last post by:
Hopefully I 'm missing something silly, but I can't find an easy way to loop all list items in a simple <ol>. I was hoping a for loop as shown below would be enough, however clicking "alert all" in...
4
by: louissan | last post by:
Hi all, I'm coming across a problem, and really do not get where it comes from. The goal: to loop over attributes read from "object" nodes in an imported XML file/flow (via XMLHTTP) and...
1
by: Praveen Naregal | last post by:
hello all, I have a problem. I have an XML file like below one.I want to loop through node by node and store or display the data . Can anybody give me a sample piece of c# code? Thanks &...
2
by: Cappy | last post by:
I am writing an XML menu structure. I have the following XML file <MenuItems> <Item> <Name>Homepa5ge</Name> <URL>/index.aspx</URL> <Alt>Return to homepage</Alt>...
2
by: Claire Reed | last post by:
Dear All, I am repeatedly encountering a problem whilst looping through XML Nodes and I am unsure as to what is going on and how I can get around it. I load the following XML document into an...
5
by: Bruce Lawrence | last post by:
I'm running Access 97 and my modules are looping if someone puts an invalid value in. The setup: 3 macros - get_clock_num, verify_clocknum, append_to_history 3 functions. each in their own...
6
by: Raul | last post by:
Hi, I have the following code, which picks up 43 different nodes from my XML document XmlNodeList amortNodes = amortDoc.SelectNodes("// TValueAmortizationSchedule/AmortizationLine"); each...
14
by: NetworkElf | last post by:
Hi all, Does anyone have some code that shows an example of how to loop through a range of IP addresses? I'm using text boxes to get a start and end value for the range. I was thinking about...
67
by: gator | last post by:
I am quite new to XML and posted a request for example code yesterday. Unfortunately, I did not do a very good job in explaining what I was looking for. Here is an example of a small piece of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.