Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl:for-each for each 3 elements problem

Reply
Thread Tools

xsl:for-each for each 3 elements problem

 
 
Tjerk Wolterink
Guest
Posts: n/a
 
      11-03-2004
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
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      11-03-2004


<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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-03-2004


Tjerk Wolterink wrote:
> 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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput 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>

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Tjerk Wolterink
Guest
Posts: n/a
 
      11-03-2004
Martin Honnen wrote:
>
>
> Tjerk Wolterink wrote:
>
>> 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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
> <xslutput 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform a 2D color image into 2 images of (R1,G1,B) at each pixelof image 1 and (R2,G2,B) at each pixel of image2 for STEREO visualization 88888 Dihedral C++ 10 12-23-2011 02:28 PM
Hash .each and different action for each key Igor Nn Ruby 7 05-28-2011 12:33 PM
FAQ 6.14 How do I process each word on each line? PerlFAQ Server Perl Misc 0 01-26-2011 11:00 PM
Array#each - getting each element and the index Pat Maddox Ruby 6 01-20-2006 04:04 PM
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM



Advertisments