xslt - applying template to sequential group of nodes

T

tuka

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
 
M

Martin Honnen

tuka said:
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>
 
J

Joe Kesselman

<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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top