xsl to group elements? [xsl newbie]

R

Rob Smegma

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
 
S

shaun

Rob Smegma said:
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>
 

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
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top