xsl to group elements? [xsl newbie]

Discussion in 'XML' started by Rob Smegma, Sep 26, 2005.

  1. Rob Smegma

    Rob Smegma Guest

    Greetings,

    I'm a beginner of XSL. Please pardon my inexperience.

    I have an xml document that has the following:

    <element id="abcdef">
    <element id="234234">
    <element id="grfgsdfg">
    <element id="ertetergsx">
    <element id="zhdthhj">
    <element id="zdtt">
    <element id="rtussj">
    <element id="fdndfgnr">
    <element id="45y745">

    I would like to translate them into groups of 3 like so:
    <group id="1">
    <element id="abcdef">
    <element id="234234">
    <element id="grfgsdfg">
    </group>

    <group id="2">
    <element id="ertetergsx">
    <element id="zhdthhj">
    <element id="zdtt">
    </group>

    <group id="3">
    <element id="rtussj">
    <element id="fdndfgnr">
    <element id="45y745">
    </group>

    What is the proper XSL to do this? I can't seem to figure this out.
    Any assistance would be a great help. Thanks in advance.

    rs
     
    Rob Smegma, Sep 26, 2005
    #1
    1. Advertising

  2. Rob Smegma

    shaun Guest

    In article <>,
    "Rob Smegma" <> wrote:

    >Greetings,
    >
    >I'm a beginner of XSL. Please pardon my inexperience.
    >
    >I have an xml document that has the following:
    >
    ><element id="abcdef">
    ><element id="234234">
    ><element id="grfgsdfg">
    ><element id="ertetergsx">
    ><element id="zhdthhj">
    ><element id="zdtt">
    ><element id="rtussj">
    ><element id="fdndfgnr">
    ><element id="45y745">
    >
    >I would like to translate them into groups of 3 like so:
    ><group id="1">
    ><element id="abcdef">
    ><element id="234234">
    ><element id="grfgsdfg">
    ></group>
    >
    ><group id="2">
    ><element id="ertetergsx">
    ><element id="zhdthhj">
    ><element id="zdtt">
    ></group>
    >
    ><group id="3">
    ><element id="rtussj">
    ><element id="fdndfgnr">
    ><element id="45y745">
    ></group>
    >
    >What is the proper XSL to do this? I can't seem to figure this out.
    >Any assistance would be a great help. Thanks in advance.
    >
    >rs


    Nasty bit of magic with the position numbering, but this will do it:

    <?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" indent="yes"/>
    <xsl:template match="root">
    <xsl:element name="rootOutput">
    <xsl:apply-templates/>
    </xsl:element>
    </xsl:template>
    <xsl:template match="element">
    <xsl:variable name="first">
    <xsl:value-of select="(position() div 2) +1"/>
    </xsl:variable>
    <xsl:if test="$first - (3 * floor($first div 3 )) = 2">
    <group id="{ceiling($first div 3)}">
    <xsl:copy-of select="/root/element[position() +
    1=$first]"/>
    <xsl:copy-of select="/root/element[position() =$first]"/>
    <xsl:copy-of select="/root/element[position() - 1
    =$first]"/>
    </group>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
     
    shaun, Sep 26, 2005
    #2
    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. Replies:
    1
    Views:
    3,691
    A. Bolmarcich
    May 27, 2005
  2. Akaketwa
    Replies:
    1
    Views:
    5,068
    impaler
    Sep 22, 2006
  3. Replies:
    0
    Views:
    574
  4. christopher taylor
    Replies:
    0
    Views:
    442
    christopher taylor
    Sep 17, 2008
  5. Replies:
    6
    Views:
    1,908
Loading...

Share This Page