View Single Post
  #1 (permalink)  
Old January 17th, 2005, 06:16 PM
rushman rushman is offline
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default preceding-sibling after sort

Hi!

I have a problem with xsl:sort. Let's say we have this input

=== XML input =====
<?xml version="1.0"?>
<items>
 <item>
  <name>something</name>
  <number>0089</number>
  <dept>management</dept>
  <subItem>subItem1</subItem>
  <subItem>subItem2</subItem>
  <subItem>subItem3</subItem>
 </item>
 <item>
  <name>anything</name>
  <number>0092</number>
  <dept>management</dept>
  <subItem>subItem4</subItem>
  <subItem>subItem5</subItem>
  <subItem>subItem6</subItem>
 </item>
</items>
==== END XML input =====

Now, the stylesheet

==== XSL Stylesheet ====

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes"/>
 <xsl:template match="items">
  <groups>
   <xsl:call-template name="itemTemplate"/>
  </groups>
 </xsl:template>
 <xsl:template name="itemTemplate">
  <xsl:for-each select="item">
   <xsl:sort data-type="text" order="ascending" select="name"/>
   <xsl:sort data-type="number" order="ascending" select="number"/>
   <group>
    <deptVal>
     <xsl:choose>
      <xsl:when test="dept=preceding-sibling::node()[1]/dept">
       <xsl:text>same</xsl:text>
      </xsl:when>
      <xsl:otherwise>
       <xsl:text>different</xsl:text>
      </xsl:otherwise>
     </xsl:choose>
    </deptVal>
    <xsl:copy-of select="."/>
   </group>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
======= END XSL ==========

Here's my actual output

==== OUTPUT FROM TRANSFORMATION ===
<?xml version="1.0" encoding="UTF-16"?>
<groups>
 <group>
  <deptVal>same</deptVal>
  <item>
   <name>anything</name>
   <number>0092</number>
   <dept>management</dept>
   <subItem>subItem4</subItem>
   <subItem>subItem5</subItem>
   <subItem>subItem6</subItem>
  </item>
 </group>
 <group>
  <deptVal>different</deptVal>
  <item>
   <name>something</name>
   <number>0089</number>
   <dept>management</dept>
   <subItem>subItem1</subItem>
   <subItem>subItem2</subItem>
   <subItem>subItem3</subItem>
  </item>
 </group>
</groups>
=== END OUTPUT ====

My problem is that the nodes are output in the order I specified in the sort, but the evaluation of the preceding-sibling follows the order BEFORE the sort (original document order).

Is there a way to force the sort BEFORE evaluating preceding-sibling in a case like mine?

Here's my desired output:

=== DESIRED OUTPUT ===
<?xml version="1.0" encoding="UTF-16"?>
<groups>
 <group>
  <deptVal>different</deptVal>
  <item>
   <name>anything</name>
   <number>0092</number>
   <dept>management</dept>
   <subItem>subItem4</subItem>
   <subItem>subItem5</subItem>
   <subItem>subItem6</subItem>
  </item>
 </group>
 <group>
  <deptVal>same</deptVal>
  <item>
   <name>something</name>
   <number>0089</number>
   <dept>management</dept>
   <subItem>subItem1</subItem>
   <subItem>subItem2</subItem>
   <subItem>subItem3</subItem>
  </item>
 </group>
</groups>

=== END DESIRED OUTPUT ====

Thanks a lot everyone!

(I wonder how Mister Kay will resolve this one...)

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
__________________
Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
Reply With Quote