472,352 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Recusion in XSL with nested XML

I have an XML file of varying nesting depth...

<catalouge>
<item id="1">
<name/>
<item id="23">
<name/>
<item id="55">
<name/>
</item>
</item>
<item id="7">
<name/>
</item>
</catalouge>

So, each item can contain many items.

I'm trying to get my XSLT to recursivly loop through each item to
print it out in block quotes eg
1 name
23 name
55 name
7 name

How can I get a <xsl:for-each> loop to go through the whole tree?

Thanks

Yours, in confusion

o0
Jul 20 '05 #1
6 2101
Hi,

Whenever you are dealing with common elements that appear at nested levels
it is usually best to avoid for-each and go for a recursive push approach
using applied templates, e.g....

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="catalouge">
<html>
<body>
<xsl:apply-templates select="item"/>
</body>
</html>
</xsl:template>

<xsl:template match="item">
<blockquote>
<xsl:value-of select="@id"/>
<xsl:text> </xsl:text>
<xsl:value-of select="name"/>
<xsl:apply-templates select="item"/>
</blockquote>
</xsl:template>
</xsl:stylesheet>

HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
"oooooo0000000" <oo***********@yahoo.com.au> wrote in message
news:41**************************@posting.google.c om...
I have an XML file of varying nesting depth...

<catalouge>
<item id="1">
<name/>
<item id="23">
<name/>
<item id="55">
<name/>
</item>
</item>
<item id="7">
<name/>
</item>
</catalouge>

So, each item can contain many items.

I'm trying to get my XSLT to recursivly loop through each item to
print it out in block quotes eg
1 name
23 name
55 name
7 name

How can I get a <xsl:for-each> loop to go through the whole tree?

Thanks

Yours, in confusion

o0

Jul 20 '05 #2
oooooo0000000 wrote:
I have an XML file of varying nesting depth...

<catalouge>
<item id="1">
<name/>
<item id="23">
<name/>
<item id="55">
<name/>
</item>
</item>
<item id="7">
<name/>
</item>
</catalouge>

So, each item can contain many items.

I'm trying to get my XSLT to recursivly loop through each item to
print it out in block quotes eg


I hope you don't mean the HTML blockquote element, which is semantically
most probably inappropriate for this purpose (as the name says, it's for
a long block of quotation). Nested lists (ol/ul) would be much better.
If you want to indent text, use CSS.
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
Jul 20 '05 #3
oo***********@yahoo.com.au (oooooo0000000) wrote in message news:<41**************************@posting.google. com>...
I have an XML file of varying nesting depth...

<catalouge>
<item id="1">
<name/>
<item id="23">
<name/>
<item id="55">
<name/>
</item>
</item>
<item id="7">
<name/>
</item>
</catalouge>

So, each item can contain many items.

I'm trying to get my XSLT to recursivly loop through each item to
print it out in block quotes eg
1 name
23 name
55 name
7 name

How can I get a <xsl:for-each> loop to go through the whole tree?


Easy; don't. Use apply-templates. If you make this a general habit you
will write much more efficient code. I think this does it in 1.0:

(A quick hack, and untested; there is probably a more efficient way
even if it works, but you get the idea.)

<xsl:template match="item">
<xsl:apply-templates select="*">
</xsl:template>

<!-- if you're doing what I think you're doing (?) -->
<xsl:template match="item/*[not(self::item)]">
<!--
one of the few occasions I'll use for-each,
and even then I'd be more inclined to use a 'repeat'
subtemplate with count(ancestor::item) supplied as a parameter
-->
<xsl:for-each select="ancestor::item">
<xsl:text> </xsl:text>
</xsl:for-each>

<xsl:value-of select="concat(parent::item/@id, ' ', name())"/>
<xsl:text>
</xsl:text><!-- output an EOL character - I hate breaking indent
style to do that, so I usually use a global
text variable called $EOL or similar -->
</xsl:template>

--
Robin Johnson
Lead Developer, enCircle Solutions Ltd.
first initial last name at encircle dot co dot uk
Jul 20 '05 #4
oo***********@yahoo.com.au (oooooo0000000) wrote in message news:<41**************************@posting.google. com>...
I have an XML file of varying nesting depth... <SNIP>
How can I get a <xsl:for-each> loop to go through the whole tree?


Ok, so I can use <xsl:for-each select="//item"> to pick up all of the
items (hurrah!) and I can use other XPATHs to get all the children of
a particular node.

But, how do I go about "unbalancing" the blockquotes?

I want

parent
child
grandchild
other child

etc. so I tried to use

<xsl:template match="/">
<xsl:for-each select="//item">
<BLOCKQUOTE>
<xsl:value-of select="title"/>
</xsl:for-each>
</BLOCKQUOTE>
</xsl:template>

and, of course, with the tags out of order it doesn't work. My brain
is telling me I'm missing something staggeringly obvious... any clues?

Thank

o0
Jul 20 '05 #5
Hello,

it is interesting to see that you are talking
about recursion although such XML structures
are just ordinary trees. Below you find a non-
recursive solution.
I'm trying to get my XSLT to recursivly loop through each item to
print it out in block quotes eg
1 name
23 name
55 name
7 name

You asked for a solution in XSL.
I hope you wont flame me if I post a solution
in a different language. This is a GNU AWK
script which I have tested:

BEGIN { XMLMODE=1 }
XMLSTARTELEM { Depth++ }
XMLSTARTELEM == "item" { id=XMLATTR["id"] }
XMLSTARTELEM == "name" { printf("%*s %s\n", 2*Depth, id, "name" ) }
XMLENDELEM { Depth-- }

As I said, this one is tested and works, producing
correctly indented lines:

1 name
23 name
55 name
7 name
How can I get a <xsl:for-each> loop to go through the whole tree?


In AWK, traversing the tree is the default behavior,
therefore no recursion is needed.
Jul 20 '05 #6
Errrrmmm, don't use for-each to begin with? ;)

"oooooo0000000" <oo***********@yahoo.com.au> wrote in message
news:41**************************@posting.google.c om...
oo***********@yahoo.com.au (oooooo0000000) wrote in message

news:<41**************************@posting.google. com>...
I have an XML file of varying nesting depth...

<SNIP>

How can I get a <xsl:for-each> loop to go through the whole tree?


Ok, so I can use <xsl:for-each select="//item"> to pick up all of the
items (hurrah!) and I can use other XPATHs to get all the children of
a particular node.

But, how do I go about "unbalancing" the blockquotes?

I want

parent
child
grandchild
other child

etc. so I tried to use

<xsl:template match="/">
<xsl:for-each select="//item">
<BLOCKQUOTE>
<xsl:value-of select="title"/>
</xsl:for-each>
</BLOCKQUOTE>
</xsl:template>

and, of course, with the tags out of order it doesn't work. My brain
is telling me I'm missing something staggeringly obvious... any clues?

Thank

o0

Jul 20 '05 #7

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

Similar topics

0
by: Glen | last post by:
I have a Struts action form which contains a bean. I am trying to display a bean retrieved from the database in this form using the nested tag. ...
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested...
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is...
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in...
29
by: Barry | last post by:
I know this is not a php question. If that bothers you, don't respond. If not, I sure could use the advice... I'm using a very abbreviated set of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.