Jorg,
Here is another solution to your question, very similar in idea to Jarno's but
may be a little simpler to follow depending on your level of comfort with XSLT.
It also has the advantage/disadvantage of being more specific to this problem
where Jarno's was much more generic.
This uses the fact that all groups would contain a <parameter> element with name
"start" (you could just as well use "end") so it matches on all the "start"
<parameters> and then looks for the associated "end" and "text". It sorts by
number (although as written doesn't output a comment) and the output should
match what you posted.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="ordering"/>
</xsl:template>
<xsl:template match="ordering">
<xsl:copy>
<xsl:apply-templates select="parameter[starts-with(@name, 'start')]" >
<xsl:sort select="translate(@name, 'start', '')" data-type="text"
order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="parameter">
<xsl:variable name="curNum" select="translate(@name, 'start', '')" />
<element>
<end>
<xsl:value-of select="../parameter[translate(@name, 'end', '') =
$curNum]/value" />
</end>
<xsl:if test="../parameter[translate(@name, 'text', '') = $curNum]/value">
<text>
<xsl:value-of select="../parameter[translate(@name, 'text', '') =
$curNum]/value" />
</text>
</xsl:if>
<start>
<xsl:value-of select="value" />
</start>
</element>
</xsl:template>
</xsl:stylesheet>
Input:
<?xml version="1.0" ?>
<ordering>
<parameter name="start2">
<value>3000</value>
</parameter>
<parameter name="text2">
<value>texthere</value>
</parameter>
<parameter name="end2">
<value>4000</value>
</parameter>
<parameter name="end1">
<value>2000</value>
</parameter>
<parameter name="start1">
<value>1000</value>
</parameter>
</ordering>
Output:
<ordering>
<element>
<end>2000</end>
<start>1000</start>
</element>
<element>
<end>4000</end>
<text>texthere</text>
<start>3000</start>
</element>
</ordering>
Hope this helps.
-Peter
-----Original Message-----
From: Jorg Heymans [mailto:Jorg(_dot_)Heymans(_at_)12snap(_dot_)com]
Sent: Thursday, July 24, 2003 3:49 AM
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] reordering elements based on...
Hi list,
(This one is doing my head in)
Say I have following input doc (ordering of the parameter nodes can be
random)
<parameter name="start2">
<value>3000</value>
</parameter>
<parameter name="text2">
<value>texthere</value>
</parameter>
<parameter name="end2">
<value>4000</value>
</parameter>
<parameter name="end1">
<value>2000</value>
</parameter>
<parameter name="start1">
<value>1000</value>
</parameter>
The number suffix of the name attribute value in the parameter node should
be used as grouping.
I would like to get following structure.
<!- parameters with ending 1 grouped into one node-->
<element>
<end>2000</end>
<start>1000</start>
</element>
<!- parameters with ending 2 grouped into one node-->
<element>
<end>4000</end>
<text>texthere</text>
<start>3000</start>
</element>
Is this doable at all? Or am I abusing XSL for what it can/should do?
Regards
Jorg Heymans
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list