473,804 Members | 4,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2185
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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="cataloug e">
<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
"oooooo0000 000" <oo***********@ yahoo.com.au> wrote in message
news:41******** *************** ***@posting.goo gle.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?

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***********@y ahoo.com.au (oooooo0000000) wrote in message news:<41******* *************** ****@posting.go ogle.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="ancesto r::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***********@y ahoo.com.au (oooooo0000000) wrote in message news:<41******* *************** ****@posting.go ogle.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 "unbalancin g" 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? ;)

"oooooo0000 000" <oo***********@ yahoo.com.au> wrote in message
news:41******** *************** ***@posting.goo gle.com...
oo***********@y ahoo.com.au (oooooo0000000) wrote in message

news:<41******* *************** ****@posting.go ogle.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 "unbalancin g" 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
2886
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. Can anyone help me? I continue to get an error message see end for stack trace. Struts config <form-bean name="newBean" type="com.NewForm" /> ...... <action path="/new-action" type="com.NewAction" name="newBean" scope="session">
6
2571
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 functions and found this Guido quote: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the
3
2407
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 { public:
6
559
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 instance, I want to be able to do 'set' operations like MyClass.MyColors = Color.Green or, a 'get', such as Color forecolor = MyClass.MyColors; I want to use an indexer so I can take parameters, such as the color type (e.g. "Foreground", "Background" etc.). With a single member function I couldn't...
8
16925
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 updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
1
2131
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
5253
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 450% slower on something as simple as a nested loop. Is this because .NET is inherently slower or does the C# compiler merely produce code that is not as well optimized as the C++ compiler?
3
3888
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 teh sample program is included bellow: #include <iostream>
29
1787
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 code to show a calendar. The idea is to simply hide the calendar when it loses focus. I'm displaying the calendar in a span tag. When that span loses focus, I recurse through its children. If focus was lost to a child, then I want to keep the calendar visible. I'm using the element.all to...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.