xsl-list
[Top] [All Lists]

RE: Using xsl:sort to sort child elements

2003-11-04 12:16:47
Thanks Charles,

As you may have noticed, I'm new to XSLT and your advice not only solved 
my problem, but enlightened me to some aspects of XSLT that were obvious 
to me.

Jim

-----Original Message-----
From: cknell(_at_)onebox(_dot_)com
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Date: Tue, 04 Nov 2003 13:04:12 -0500
Subject: RE: [xsl] Using xsl:sort to sort child elements

I think that you will find that the context node at which you are
applying the template is the ZIP code of one address. Since, by
definition, there is only one ZIP code per <ZipCode> element, sorting
at that point will do exactly what you have noticed.

Change your template so it looks like this:

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema";>
<xsl:output method="xml" indent="yes" standalone="yes"
omit-xml-declaration="no" encoding="UTF-8"/>

<xsl:template match="EntryData/Applicant">
  <xsl:copy>
    <xsl:apply-templates>
     <xsl:sort select="ZipCode"/>
   </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

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

<xsl:template match="*|@*|text()">
 <xsl:copy>
   <xsl:apply-templates select="*|@*|text()">
   </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

-- 
Charles Knell
cknell(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     Jim McDowall <Jim(_dot_)Mcdowall(_at_)thesoftlife(_dot_)com>
Sent:     Tue, 04 Nov 2003 11:29:36 -0600
To:       XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject:  [xsl] Using xsl:sort to sort child elements

I have examined the numerous examples of the xsl:sort instruction but 
haven't seen one that describes the technique to use for what I need to
do.  

I need to sort child elements of the document that I am transforming.
Here is a very very simple example of what I'm trying to accomplish.  
Given this document: 

<EntryData>
      <Applicant>
              <FirstName>Ralph</FirstName>
              <LastName>Smith</LastName>
              <Address>
                      <Type>Business</Type>
                      <City>Chicago</City>
                      <ZipCode>60011</ZipCode>
              </Address>
              <Address>
                      <Type>Regional</Type>
                      <City>Springfield</City>
                      <ZipCode>62103</ZipCode>
              </Address>
              <Address>
                      <Type>Corporate</Type>
                      <City>New York</City>
                      <ZipCode>10011</ZipCode>
              </Address>
      </Applicant>
</EntryData>

I would like to transform this document sorting the "Address" elements 
within the document by zip code.

Here's an example of a style sheet that I've tried but the result tree 
remains in it's original state... 

<?xml version="1.0"?>
<!--XML Declaration -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema";> 
<xsl:output method="xml" indent="yes" standalone="yes"
omit-xml-declaration="no" encoding="UTF-8"/> 

<xsl:template match="EntryData/Applicant/Address">
  <xsl:copy>
    <xsl:apply-templates> 
     <xsl:sort select="ZipCode"/>
   </xsl:apply-templates> 
  </xsl:copy>
</xsl:template>

<xsl:template match="*|@*|text()">
 <xsl:copy>
   <xsl:apply-templates select="*|@*|text()">
   </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Please advise.  

Thanks,
Jim McDowall


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>