Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 17th, 2005, 06:16 PM
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
  #2 (permalink)  
Old January 17th, 2005, 07:14 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In general to do sorting then grouping you need to do a two-phase transformation. You can do this using two stylesheets, or using a single stylesheet if you write the result of the first phase to a variable and then (assuming XSLT 1.0) convert this variable to a node-set using the xx:node-set() extension function. This means that after sorting you need to copy the sorted records to the intermediate tree using xsl:copy-of; the preceding-sibling axis on the copied nodes will reflect their position in the intermediate tree. Remember that axes always reflect relationships among the nodes in a tree: sorting only changes the processing order of the nodes, it doesn't modify the original tree.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old January 18th, 2005, 10:35 AM
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

Hi Mr. Kay!

I had the same idea yesterday when I was coming home from work. Your reply confirms that I'm on the right path.

Your answer raise another question for me. Is it possible with XSLT 1.0 and without extensions to create the intermediate tree? (I honestly haven't tried yet, but I'm going to right away)

Thanks again for your time.
Reply With Quote
  #4 (permalink)  
Old January 18th, 2005, 11:08 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Creating the intermediate tree is no problem in XSLT 1.0. But the resulting type is "result tree fragment" and 1.0 has severe restrictions on how you can process an RTF. This is why the xx:node-set() extension was invented, to "convert" the RTF to an ordinary node-set (the root node of a tree).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using preceding-sibling mcanne98 Infopath 0 September 11th, 2008 11:09 PM
Preceding-sibling question. lafilip XSLT 7 March 5th, 2007 12:35 PM
preceding-sibling descendant xaun XSLT 3 February 28th, 2006 02:48 PM
preceding-sibling jonesyp XSLT 2 November 22nd, 2005 12:29 PM
preceding-sibling behaviour andrewcday XSLT 6 December 3rd, 2004 03:24 PM



All times are GMT -4. The time now is 09:11 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.