xslt - applying template to sequential group of nodes

Discussion in 'XML' started by tuka, Jun 21, 2007.

  1. tuka

    tuka Guest

    Hi,

    I have a problem I would like help solving:

    My xml:

    <node id=1>
    <nodeContent>xyz</nodeContent>
    </node>
    <node id=2>
    <nodeContent>xyz2</nodeContent>
    </node>
    <node id=3>
    <nodeContent>xyz3</nodeContent>
    </node>
    <node id=4>
    <nodeContent>xyz4</nodeContent>
    </node>

    I would like to use xslt to apply a <div> tag between the 2nd and 4th
    tag so that my the output will be

    <div>
    xyz2
    xyz3
    xyz4
    </div>



    I have tried using something like

    <xsl:template match=""> </xsl:template> but it seems that I cannot
    set an opening div tag for 2 and a closing div tag for node 4 without
    errors. i.e. the xml withing the xsl:template tag must be well
    formed... Not much luck with if tags either...

    TIA
     
    tuka, Jun 21, 2007
    #1
    1. Advertising

  2. tuka wrote:

    > My xml:
    >
    > <node id=1>
    > <nodeContent>xyz</nodeContent>
    > </node>
    > <node id=2>
    > <nodeContent>xyz2</nodeContent>
    > </node>
    > <node id=3>
    > <nodeContent>xyz3</nodeContent>
    > </node>
    > <node id=4>
    > <nodeContent>xyz4</nodeContent>
    > </node>
    >
    > I would like to use xslt to apply a <div> tag between the 2nd and 4th
    > tag so that my the output will be
    >
    > <div>
    > xyz2
    > xyz3
    > xyz4
    > </div>


    Those node elements need to have a parent element so write a template
    for that parent element, assuming its tag name is 'some-element' use e.g.
    <xsl:template match="some-element">
    <div>
    <xsl:apply-templates select="node[@id &gt;= 2 and @id &lt;=
    4]/nodeContent"/>
    </div>
    </xsl:template>

    <xsl:template match="nodeContent">
    <xsl:value-of select="concat(., '
    ')"/>
    </xsl:template>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 21, 2007
    #2
    1. Advertising

  3. tuka

    tuka Guest

    Thanks,

    It worked nicely
    Tuka
     
    tuka, Jun 21, 2007
    #3
  4. > <xsl:template match=""> </xsl:template>

    The empty string is not a legal match patern.

    > errors. i.e. the xml withing the xsl:template tag must be well
    > formed.


    XSLT is an XML language. XML must be well formed. Period. You can't
    generate the open tag and close tag in separate templates. Generate them
    at a higher-level template than the one generating the content.

    Tutorials.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 22, 2007
    #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. asd
    Replies:
    3
    Views:
    452
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    537
    gavnosis
    Aug 2, 2003
  3. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    428
    Patrick TJ McPhee
    Jun 17, 2004
  4. jon|k
    Replies:
    2
    Views:
    9,770
    jon|k
    Jan 20, 2006
  5. Liam
    Replies:
    1
    Views:
    759
    Pavel Lepin
    Jul 17, 2007
Loading...

Share This Page