[xslt] problem

T

Tjerk Wolterink

i have xml like this:

<data>
<item type="a">
1
</item>
<item type="a">
2
</item>
<item type="b">
3
</item>
<item type="a">
4
</item>
<item type="a">
5
</item>
<item type="b">
6
</item>
<item type="a">
7
</item>
</data>

i want some xsl toget this for $type=a:

<output>
<row>
<data>1</data>
<data>2</data>
</row>
<row>
<data>4</data>
<data>5</data>
</row>
<row>
<data>7</data>
<data></data>
</row>
</output>

and for $type=b this as output:

<output>
<row>
<data>3</data>
<data>6</data>
</row>
</output>


my xsl sofar:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0">

<xsl:param name="type"/>

<xsl:template match="/data">
<xsl:for-each select="item[xc:type=$type][(position() mod 2)=1]">
<row>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="following-sibling::*[position() &lt; 2]"/>
</row>
</xsl:for-each>
</xsl:template>

<xsl:template match="xc:medewerker">
<data>
<xsl:value-of select="."/>
</data>
</xsl:template>

<xsl:stylesheet>


But this does not work, because some times items of type b
are chosen when $type equals a.

please help
 
S

Soren Kuula

But this does not work, because some times items of type b
are chosen when $type equals a.

please help

Try

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="type" select="'a'"/>

<xsl:template match="/data">
<xsl:for-each select="item[@type=$type][position() mod 2 = 1]">
<row>
<data><xsl:apply-templates select="."/></data>
<data><xsl:apply-templates
select="following-sibling::*[@type=$type][position() &lt;2]"/>$
</row>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

(what you following-sibling saw was not the successors in the list
selected in for-each, but just the siblings of the current node...)

Soren
 
?

=?ISO-8859-1?Q?Joachim_Wei=DF?=

Tjerk said:
<xsl:template match="/data">
<xsl:for-each select="item[xc:type=$type][(position() mod 2)=1]">
<row>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="following-sibling::*[position()
&lt; 2]"/>
</row>
</xsl:for-each>
</xsl:template>


Soren was quicker!

However here is my solution without foreach loops, empty element at the
list and last but not least only one representation of the item template.


Hope it helps!


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:eek:utput method="xml" />

<xsl:param name="type">a</xsl:param>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="data">
<xsl:element name="output">
<!-- simply select every second element -->
<xsl:apply-templates select="item[@type=$type][not(position() mod
2=0)]" mode="loop"/>
</xsl:element>
</xsl:template>


<!-- item builds a group (remember only every second element is
selected) -->
<xsl:template match="item" mode="loop">
<xsl:element name="group">
<!-- buliding the data element -->
<xsl:apply-templates select="." mode="item" />
<!-- if there is no next item bulild an empty data element -->
<xsl:if test="not(following-sibling::item[@type=$type][1])">
<xsl:element name="data"/>
</xsl:if>
<!-- now we apply a template for exactly the next item -->
<xsl:apply-templates
select="following-sibling::item[@type=$type][1]" mode="item"/>

</xsl:element>
</xsl:template>

<!-- our item template -->
<xsl:template match="item" mode="item">
<xsl:element name="data">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>

</xsl:stylesheet>
 
S

Soren Kuula

Hi Soren,
<xsl:for-each select="item[@type=$type][position() mod 2 = 1]">
<row>
<data><xsl:apply-templates select="."/></data>
<data><xsl:apply-templates
select="following-sibling::*[@type=$type][position() &lt;2]"/>$

Oops, how did that final $ sign end up there? It's not supposed to be
there, must be a typo.
Soren
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top