Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xslt - applying template to sequential group of nodes

Reply
Thread Tools

xslt - applying template to sequential group of nodes

 
 
tuka
Guest
Posts: n/a
 
      06-21-2007
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      06-21-2007
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/
 
Reply With Quote
 
 
 
 
tuka
Guest
Posts: n/a
 
      06-21-2007
Thanks,

It worked nicely
Tuka

 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      06-21-2007
> <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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Sequential Numbering Liam XML 1 07-17-2007 12:30 PM
xslt: for-each group, grouping by child nodes? jon|k XML 2 01-20-2006 09:23 AM
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments