[xsl] RE: loop elements based on attribute

Subject: [xsl] RE: loop elements based on attribute
From: "Kjellaug Johansen" <kjellaug.johansen@xxxxx>
Date: Fri, 15 Aug 2008 16:42:28 +0200
Hi.

My problem just turned to be a bit bigger.

In an xslt-file I use templates for each match on different elements.
When I reach a Pkt-element I call the template "pktTemplate". How can I
get the right result? I'm almost there...but the problem is that the
first Pkt-element that matches (where the template-call is done) is
never listed in the result. But the siblings are listed at the right
way.

My source XML:
<test>
	<A>test</A>
 	<Pkt type="pkt">Text</Pkt>
 	<Pkt type="uavs">Text 1</Pkt>
 	<Pkt type="pkt">More text</Pkt>
 	<Pkt type="uavs">More text 1</Pkt>
 	<Pkt type="uavs">More text 2</Pkt>
 	<B>test</B>
 	<A>test</A>
	<Pkt type="num">Lot of text</Pkt>
	<Pkt type="num">Tons of text</Pkt>
	<Pkt type="uavs">Tons of text 1</Pkt>
 </test>

My result today (which is wrong):
<List>
	<Pkt>
		<A>Text 1</A>
	</Pkt>
	<Pkt>
		<A>More text</A>
		<A>More text 1</A>
		<A>More text 2</A>
	</Pkt>
</List>
<List>
	<Pkt>
		<A>Tons of text</A>
		<A>Tons of text 1</A>
	</Pkt>
</List>

The result I want (the right one):
<List>
	<Pkt>
		<A>Text</A>
		<A>Text 1</A>
	</Pkt>
	<Pkt>
		<A>More text</A>
		<A>More text 1</A>
		<A>More text 2</A>
	</Pkt>
</List>
<List>
	<Pkt>
		<A>Lot of text</A>
	</Pkt>
	<Pkt>
		<A>Tons of text</A>
		<A>Tons of text 1</A>
	</Pkt>
</List>

My xsl: (I use xsl 2.0)

//some tests on different elements, when I match a Pkt I do the
following:
<xsl:call-template name="pktTemplate"/>
<xsl:template name="pktTemplate">
	<xsl:if test="not(preceding-sibling::*[1][self::Pkt])">
		<List>
			<xsl:for-each-group
select="following-sibling::Pkt
			 [preceding-sibling::*[1][self::Pkt]][generate-
id(preceding-sibling::Pkt[not(preceding-
sibling::*[1][self::Pkt])][1])= generate-
id(current())]" group-starting-with="Pkt[not(@type =
'uavs')]">
				<Pkt>

					<xsl:for-each
select="current-group()">
						<A><xsl:value-of
select="." /></A>
					</xsl:for-each>
				</Pkt>
			</xsl:for-each-group>
		</List>
	</xsl:if>
</xsl:template>

Current Thread