looping through names of nodes and sub nodes

K

k.a.bouton

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
 
J

Joris Gillis

Hi,

Tempore 15:48:16 said:
<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,
 
B

bouton

that seems to work prefect.Thanks. it was the generate_id where I got
lost. thanks again
K
 
B

bouton

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
 
J

Joris Gillis

Hi,

Tempore 17:37:44 said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,830
Latest member
HeleneMull

Latest Threads

Top