xsl:for-each for each 3 elements problem

Discussion in 'XML' started by Tjerk Wolterink, Nov 3, 2004.

  1. 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
     
    Tjerk Wolterink, Nov 3, 2004
    #1
    1. Advertising

  2. <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
     
    David Carlisle, Nov 3, 2004
    #2
    1. Advertising

  3. 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 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>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 3, 2004
    #3
  4. 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 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
     
    Tjerk Wolterink, Nov 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. f
    Replies:
    2
    Views:
    498
    Arto V. Viitanen
    Nov 20, 2003
  2. Sparko
    Replies:
    0
    Views:
    410
    Sparko
    Jul 9, 2004
  3. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  4. Rob Smegma
    Replies:
    1
    Views:
    1,818
    shaun
    Sep 26, 2005
  5. Max
    Replies:
    1
    Views:
    543
    Martin Honnen
    Aug 19, 2007
Loading...

Share This Page