Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2009, 07:31 AM
Registered User
 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple sorting issue

Thought this would be a simple issue to solve. I want to simply kept the input document structure intact and sort the customer nodes based on the CompanyName. Runtime target is .NET 2.0. I've been testing in Stylus Studio.

The document structure is reproduced correctly but I haven't been able to find the correct selects to make the sort happen.

Thanks.

Sample input:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xmldata>
  <Customers>
    <CustomerID>15</CustomerID>
    <AccessKey>P</AccessKey>
    <BillingAddress1>151 Duck Blind Place</BillingAddress1>
    <City>Myrtle Beach</City>
    <CompanyName>Wayside Kennel Gear</CompanyName>
    <EmailAddress>[email protected]</EmailAddress>
    <PostalCode>29588</PostalCode>
    <State>SC</State>
  </Customers>
  <Customers>
    <CustomerID>21</CustomerID>
    <AccessKey>P</AccessKey>
    <BillingAddress1>8794 Easton Road</BillingAddress1>
    <City>Ottsville</City>
    <CompanyName>The Yuppie Puppy</CompanyName>
    <EmailAddress>[email protected]</EmailAddress>
    <PostalCode>18092</PostalCode>
    <State>PA</State>
  </Customers>
  <Customers>
    <CustomerID>22</CustomerID>
    <AccessKey>P</AccessKey>
    <BillingAddress1>305 Gordon Drive</BillingAddress1>
    <City>Exton</City>
    <CompanyName>Pickering Valley Feed</CompanyName>
    <EmailAddress>[email protected]</EmailAddress>
    <PostalCode>19341</PostalCode>
    <State>PA</State>
  </Customers>
</xmldata>
XSLT
Code:
<?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="xmldata/Customers">
		<xsl:apply-templates>
		  <xsl:sort select="CompanyName"/>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="/">
		<xsl:copy-of select="*" >
		</xsl:copy-of>
	</xsl:template>

</xsl:stylesheet>
 
Old August 4th, 2009, 07:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You confused me, and perhaps yourself, by using the element name "Customers" to represent a single customer. You can't sort the customers in the rule that processes each individual customer, it has to be done in the rule that processes the parent element. And your second template rule is wrong because it simply copies the input document intact without every firing the template rules for the children. You only need one template rule here:

Code:
<xsl:template match="xmldata">
  <xsl:for-each select="Customers">
     <xsl:sort select="CompanyName"/>
     <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:template>
Of course you can use an apply-templates if you want to do further processing of the customer elements, but if you just want to copy them, the above is simpler.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting issue nguna XSLT 3 July 28th, 2009 07:45 AM
sorting issue JohnBampton XSLT 10 February 23rd, 2009 08:13 AM
A simple Sorting question bonekrusher XSLT 2 November 25th, 2007 09:40 AM
sorting issue moin.khan Struts 0 June 21st, 2007 09:37 AM
Simple 'passing variables between forms' issue... overture C# 4 October 14th, 2005 02:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.