xsl:for-each for each 3 elements problem

  • Thread starter Tjerk Wolterink
  • Start date
T

Tjerk Wolterink

Hello, i've an xml doc like this:

<doc>
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item>
<item>e</item>
<item>f</item>
<item>g</item>
</doc>

Now i want this to be transformed to something like this:

<doc2>
<three>
<item>a</item>
<item>b</item>
<item>c</item>
</three>
<three>
<item>d</item>
<item>e</item>
<item>f</item>
</three>
<three>
<item>g</item>
</three>
</doc2>

Well i think i need a for-each, but does for-each support, for each,
lets say three elements??

I now have something like this:

<xsl:for-each select="item">
xsl:if test="position() mod 3=0 and position()!=0">
</three>
</xsl:if>
<xsl:if test="position() mod 3=0">
<three>
</xsl:if>
<item>
<xsl:value-of select="."/>
</item>
</xsl:for-each>

But this is no valid xml, please help
 
D

David Carlisle

<xsl:for-each select="item[position() mod 3 = 1]">
<three>
<xsl:copy-of select=".|following-sibling::item[position()&lt;3]"/>
</three>
</xsl:for-each>

David
 
M

Martin Honnen

Tjerk said:
Hello, i've an xml doc like this:

<doc>
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item>
<item>e</item>
<item>f</item>
<item>g</item>
</doc>

Now i want this to be transformed to something like this:

<doc2>
<three>
<item>a</item>
<item>b</item>
<item>c</item>
</three>
<three>
<item>d</item>
<item>e</item>
<item>f</item>
</three>
<three>
<item>g</item>
</three>
</doc2>

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

<xsl:eek:utput method="xml" encoding="UTF-8" indent="yes" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="doc">
<doc2>
<xsl:for-each select="item[position() mod 3 = 1]">
<three>
<xsl:apply-templates select=". |
following-sibling::item[position() &lt; 3]" />
</three>
</xsl:for-each>
</doc2>
</xsl:template>

</xsl:stylesheet>
 
T

Tjerk Wolterink

Martin said:
Tjerk said:
Hello, i've an xml doc like this:

<doc>
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item>
<item>e</item>
<item>f</item>
<item>g</item>
</doc>

Now i want this to be transformed to something like this:

<doc2>
<three>
<item>a</item>
<item>b</item>
<item>c</item>
</three>
<three>
<item>d</item>
<item>e</item>
<item>f</item>
</three>
<three>
<item>g</item>
</three>
</doc2>


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

<xsl:eek:utput method="xml" encoding="UTF-8" indent="yes" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="doc">
<doc2>
<xsl:for-each select="item[position() mod 3 = 1]">
<three>
<xsl:apply-templates select=". |
following-sibling::item[position() &lt; 3]" />
</three>
</xsl:for-each>
</doc2>
</xsl:template>

</xsl:stylesheet>

nice really nice, i did not think of that
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top