[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: reordering elements based on...

Subject: RE: reordering elements based on...
From: "Jacoby, Peter R." <PJACOBY@xxxxxxxxxxxx>
Date: Thu, 24 Jul 2003 14:31:18 -0400
xsl reordering elements
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.Heymans@xxxxxxxxxx]
Sent: Thursday, July 24, 2003 3:49 AM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.