Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Recursive nested elements woe (http://www.velocityreviews.com/forums/t167358-recursive-nested-elements-woe.html)

GR33DY 06-24-2004 04:42 PM

Recursive nested elements woe
 
Hi every-one,

(Deleted and reposted to avoid sp@m)

I have an XML file that looks like this:

_____________________________XML__________________ _______________

<root>
<nextlevel>
<init>
<element1>
Content
</element1>
<element2>
More content
</element2>
...etc
<message>
<element1>
Content
</element1>
<element2>
More content
</element2>
...etc
<message>
<element1>
Content
</element1>
<element2>
More content
</element2>
...etc
</message>
</message>
</init>
</nextlevel>
</root>

__________________________________________________ _______________

The <message> nodes may well contain other <message> nodes which could
contain other <message> nodes etc. etc. ad infinitum(well, almost!)

My problem is that when it comes to the XSL, I want to nest these nodes
and indent them and this is what I have so far:

_____________________________XSL__________________ _______________
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="init">
<xsl:value-of select="element1"/>
<xsl:value-of select="element2"/><br />
</xsl:template>

<xsl:template name ="message" match="message">
<blockquote>
<xsl:value-of select="element1"/>
<xsl:value-of select="element2"/>
<xsl:for-each select="message[count(descendant::message)!='0']">
<xsl:call-template name="message"/>
</xsl:for-each>
</blockquote>
</xsl:template>

__________________________________________________ _______________

However, this only nests up to and including the second <message> node
and no further. Any help appreciated :)

Tom.

GR33DY 06-25-2004 11:55 AM

Re: Recursive nested elements woe
 
Cat wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Thu, 24 Jun 2004 17:42:08 +0100, GR33DY wrote:
>
>>_____________________________XSL________________ _________________
>><xsl:template match="/">
>> <html>
>> <body>
>> <xsl:apply-templates/>
>> </body>
>> </html>
>></xsl:template>
>>
>><xsl:template match="init">
>> <xsl:value-of select="element1"/>
>> <xsl:value-of select="element2"/><br />
>></xsl:template>
>>
>><xsl:template name ="message" match="message">
>><blockquote>
>> <xsl:value-of select="element1"/>
>> <xsl:value-of select="element2"/>
>> <xsl:for-each select="message[count(descendant::message)!='0']">
>> <xsl:call-template name="message"/>
>> </xsl:for-each>
>></blockquote>
>></xsl:template>
>>
>>________________________________________________ _________________

>
> Tom,
> Wouldn't it be better/simplier to do the recursion with the normal
> xsl:apply-templates like so.
>
> ============= XSLT ================
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:template match="root">
> <html>
> <body>
> <xsl:apply-templates/>
> </body>
> </html>
> </xsl:template>
>
> <xsl:template match="init|nextlevel">
> <xsl:apply-templates/>
> </xsl:template>
>
> <xsl:template match="element1|element2">
> <xsl:value-of select="."/>
> </xsl:template>
>
> <xsl:template match="message">
> <blockquote>
> <xsl:apply-templates/>
> </blockquote>
> </xsl:template>
> </xsl:stylesheet>
> ===================================
> Is this what you were looking for?
> - --
> Cat
>
> http://www.ratrobot.com/writing/tiger/ TradeMark Tiger is an economic
> argument for conservation. Why not use nature to save itself?
> Fri Jun 25 21:55:01 UTC 2004
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (GNU/Linux)
>
> iD4DBQFA3J81KHRjYtwQ1QARApozAJipLbIw+zFzA5t+BiSYym 9dBYYrAKCxdHlc
> u12mBgs54ZzHz18we4IPzA==
> =xh+N
> -----END PGP SIGNATURE-----
>

That's nice and simple!
My solutions tend to get more and more complicated :)
How would I now go about adding some formatting to the element1 &
element2 content?

Thanks for your help, it's very much appreciated.

GR33DY 06-25-2004 12:15 PM

Re: Recursive nested elements woe
 
Cat wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Thu, 24 Jun 2004 17:42:08 +0100, GR33DY wrote:
>
>>_____________________________XSL________________ _________________
>><xsl:template match="/">
>> <html>
>> <body>
>> <xsl:apply-templates/>
>> </body>
>> </html>
>></xsl:template>
>>
>><xsl:template match="init">
>> <xsl:value-of select="element1"/>
>> <xsl:value-of select="element2"/><br />
>></xsl:template>
>>
>><xsl:template name ="message" match="message">
>><blockquote>
>> <xsl:value-of select="element1"/>
>> <xsl:value-of select="element2"/>
>> <xsl:for-each select="message[count(descendant::message)!='0']">
>> <xsl:call-template name="message"/>
>> </xsl:for-each>
>></blockquote>
>></xsl:template>
>>
>>________________________________________________ _________________

>
> Tom,
> Wouldn't it be better/simplier to do the recursion with the normal
> xsl:apply-templates like so.
>
> ============= XSLT ================
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:template match="root">
> <html>
> <body>
> <xsl:apply-templates/>
> </body>
> </html>
> </xsl:template>
>
> <xsl:template match="init|nextlevel">
> <xsl:apply-templates/>
> </xsl:template>
>
> <xsl:template match="element1|element2">
> <xsl:value-of select="."/>
> </xsl:template>
>
> <xsl:template match="message">
> <blockquote>
> <xsl:apply-templates/>
> </blockquote>
> </xsl:template>
> </xsl:stylesheet>
> ===================================
> Is this what you were looking for?
> - --
> Cat
>
> http://www.ratrobot.com/writing/tiger/ TradeMark Tiger is an economic
> argument for conservation. Why not use nature to save itself?
> Fri Jun 25 21:55:01 UTC 2004
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (GNU/Linux)
>
> iD4DBQFA3J81KHRjYtwQ1QARApozAJipLbIw+zFzA5t+BiSYym 9dBYYrAKCxdHlc
> u12mBgs54ZzHz18we4IPzA==
> =xh+N
> -----END PGP SIGNATURE-----
>

That's lovely and simple, my efforts get recursively more complicated ;)
Thanks for your help.
Tom

Cat 06-25-2004 09:55 PM

Re: Recursive nested elements woe
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, 24 Jun 2004 17:42:08 +0100, GR33DY wrote:
> _____________________________XSL__________________ _______________
> <xsl:template match="/">
> <html>
> <body>
> <xsl:apply-templates/>
> </body>
> </html>
> </xsl:template>
>
> <xsl:template match="init">
> <xsl:value-of select="element1"/>
> <xsl:value-of select="element2"/><br />
> </xsl:template>
>
> <xsl:template name ="message" match="message">
> <blockquote>
> <xsl:value-of select="element1"/>
> <xsl:value-of select="element2"/>
> <xsl:for-each select="message[count(descendant::message)!='0']">
> <xsl:call-template name="message"/>
> </xsl:for-each>
> </blockquote>
> </xsl:template>
>
> __________________________________________________ _______________

Tom,
Wouldn't it be better/simplier to do the recursion with the normal
xsl:apply-templates like so.

============= XSLT ================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="root">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="init|nextlevel">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="element1|element2">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="message">
<blockquote>
<xsl:apply-templates/>
</blockquote>
</xsl:template>
</xsl:stylesheet>
===================================
Is this what you were looking for?
- --
Cat

http://www.ratrobot.com/writing/tiger/ TradeMark Tiger is an economic
argument for conservation. Why not use nature to save itself?
Fri Jun 25 21:55:01 UTC 2004
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD4DBQFA3J81KHRjYtwQ1QARApozAJipLbIw+zFzA5t+BiSYym 9dBYYrAKCxdHlc
u12mBgs54ZzHz18we4IPzA==
=xh+N
-----END PGP SIGNATURE-----



All times are GMT. The time now is 07:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57