RE: [xsl] Selection of every second node

Subject: RE: [xsl] Selection of every second node
From: "Corey Spitzer" <cspitzer@xxxxxxxxx>
Date: Tue, 21 Aug 2001 09:09:57 -0500
Another approach:

<xsl:template match="node" mode="trash">
<xsl:apply-templates select="another" mode="trash"/>
<xsl:apply-templates select="following-sibling::node[1]" mode="nottrash"/>
</xsl:template>

<xsl:template match="node" mode="nottrash">
<xsl:apply-templates select="another" mode="nottrash"/>
<xsl:apply-templates select="following-sibling::node[1]" mode="trash"/>
</xsl:template>

<xsl:template match="another" mode="trash">
<li><xsl:value-of select="."/></li>
</xsl:template>

<xsl:template match="another" mode="nottrash">
<li type=disc><xsl:value-of select="."/></li>
</xsl:template>













-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Conny Kreyßel
Sent: Tuesday, August 21, 2001 8:41 AM
To: 'Jeni Tennison'
Cc: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: AW: [xsl] Selection of every second node


Year this works, thanks.

But now I have another question.

I would now apply a attribute (type="disc") in the "childs" template (<li>)
if the on every second parent entry (<node>) like this:

<root>
	<node>
		<another>1</another>
		<another>1</another>
		<another>1</another>
	</node>
	<node>
		<another>2</another>
		<another>2</another>
		<another>2</another>
	</node>
	<node>
		<another>3</another>
		<another>3</another>
		<another>3</another>
	</node>
	<node>
		<another>4</another>
		<another>4</another>
		<another>4</another>
	</node>
</root>

<xsl:template match="node">
	<ul>
		<xsl:apply-templates select="another" />
	</ul>
</xsl:template>

<xsl:template match="another">
	<li>
		<xsl:if test="boolean(parent::node()[(position() mod 2) =
0])">
			<xsl:attribute name="type">disc</xsl:attribute>
		</xsl:if>
		<xsl:value-of select="." />
	</li>
</xsl:template>

The Result should be:

<ul>
	<li>1</li>
	<li>1</li>
	<li>1</li>
</ul>
<ul>
	<li type="disc">2</li>
	<li type="disc">2</li>
	<li type="disc">2</li>
</ul>
<ul>
	<li>3</li>
	<li>3</li>
	<li>3</li>
</ul>
<ul>
	<li type="disc">4</li>
	<li type="disc">4</li>
	<li type="disc">4</li>
</ul>

Can you help me. My template doesnt work - I dont know why?


Thank you

COnny

> -----Ursprüngliche Nachricht-----
> Von: Jeni Tennison [mailto:mail@xxxxxxxxxxxxxxxx]
> Gesendet: Dienstag, 21. August 2001 14:03
> An: Conny Kreyßel
> Cc: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Betreff: Re: [xsl] Selection of every second node
>
>
> Hi Conny,
>
> > I hope you can help me. How can I make a template that match with
> > every second node?
>
> You need to test the position() of the node element amongst the other
> node elements in its parent root element. Fortunately, this is what
> the match pattern does by default. You can look at the position() mod
> 2 to find the even node elements - if it equals 0 then the node
> element is an even node. So try:
>
> <xsl:template match="node[(position() mod 2) = 0]">
>   ...
> </xsl:template>
>
> or:
>
> <xsl:template match="node[not(position() mod 2)]">
>   ...
> </xsl:template>
>
> Alternatively, you could have a template that matched all node
> elements, but only apply templates to every second node element, with:
>
>   <xsl:apply-templates select="node[not(position() mod 2)]" />
>
> Cheers,
>
> Jeni
>
> ---
> Jeni Tennison
> http://www.jenitennison.com/
>

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread