Recursive nested elements woe

Discussion in 'XML' started by GR33DY, Jun 24, 2004.

  1. GR33DY

    GR33DY Guest

    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, Jun 24, 2004
    #1
    1. Advertising

  2. GR33DY

    GR33DY Guest

    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+BiSYym9dBYYrAKCxdHlc
    > 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, Jun 25, 2004
    #2
    1. Advertising

  3. GR33DY

    GR33DY Guest

    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+BiSYym9dBYYrAKCxdHlc
    > u12mBgs54ZzHz18we4IPzA==
    > =xh+N
    > -----END PGP SIGNATURE-----
    >

    That's lovely and simple, my efforts get recursively more complicated ;)
    Thanks for your help.
    Tom
     
    GR33DY, Jun 25, 2004
    #3
  4. GR33DY

    Cat Guest

    -----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+BiSYym9dBYYrAKCxdHlc
    u12mBgs54ZzHz18we4IPzA==
    =xh+N
    -----END PGP SIGNATURE-----
     
    Cat, Jun 25, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    529
    Paul Clement
    Sep 12, 2005
  2. GR33DY
    Replies:
    0
    Views:
    591
    GR33DY
    Jun 24, 2004
  3. Alexander Schmolck

    re.sub replacement text \-escapes woe

    Alexander Schmolck, Feb 13, 2004, in forum: Python
    Replies:
    4
    Views:
    381
    Alexander Schmolck
    Feb 14, 2004
  4. import woe

    , May 19, 2006, in forum: Python
    Replies:
    4
    Views:
    356
    Terry Hancock
    May 19, 2006
  5. Han

    PreRenderComplete event woe

    Han, Dec 22, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,234
Loading...

Share This Page