xsl:for-each for each 3 elements problem

Tjerk Wolterink

Usenet Archive
Joined
Sep 18, 2004
Messages
218
Reaction score
0
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
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
<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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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>
 

Tjerk Wolterink

Usenet Archive
Joined
Sep 18, 2004
Messages
218
Reaction score
0
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

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top