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

Re: sorting on data referenced with document()??

Subject: Re: sorting on data referenced with document()??
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 16 Sep 2002 22:15:11 -0700 (PDT)
moby turtle video
--- "Carter, Will" <WCarter at envestnetpmc dot com> wrote:
 
> Hi,
> 
> I have a sorting problem when I use xsl to combine data from 2 xml
> files into one html output.
> 
> here is my xml file 1 (xml1.xml):
> -----------------------
> <people>
> 	<person name="george">
> 		<cat>cat-zoro</cat>
> 		<dog>dog-butch</dog>
> 		<fish>fish-jaws</fish>
> 	</person>
> 	<person name="jennifer">
> 		<cat>cat-felix</cat>
> 		<dog>dog-fido</dog>
> 		<fish>fish-moby</fish>
> 	</person>
> 	<person name="simon">
> 		<cat>cat-tom</cat>
> 		<dog>dog-scooby</dog>
> 		<fish>fish-conroy</fish>
> 	</person>
> </people>
> -----------------------
> 
> here is my xml file 2 (xml2.xml):
> -----------------------
> <people>
> 	<person name="george">
> 		<turtle>turtle-greeny</turtle>
> 	</person>
> 	<person name="jennifer">
> 		<turtle>turtle-browny</turtle>
> 	</person>
> 	<person name="simon">
> 		<turtle>turtle-red</turtle>
> 	</person>
> </people>
> -----------------------
> 
> here is my stylesheet:
> -----------------------
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns="http://www.w3.org/tr/REC-html40"; version="2.0">
> <xsl:key name="turtleByOwner" match="//turtle" use="../@name" />
> <xsl:template match="/">
> <table border="1" cellspacing="0" cellpadding="0">
> 	<xsl:for-each select="people/person">
> 	<xsl:sort select="cat"/>
> 	<tr>
> 		<td colspan="7">Person: <xsl:value-of select="@name"/></td>
> 	</tr>
> 	<tr>
> 		<td><xsl:value-of select="cat"/></td>
> 		<td><xsl:value-of select="dog"/></td>
> 		<td><xsl:value-of select="fish"/></td>
> 		<xsl:variable name="ownerName" select="@name"/>
> 		<td>
> 		<xsl:for-each select="document('xml2.xml')">
> 		<xsl:value-of select="key('turtleByOwner', $ownerName)"
> 		</xsl:for-each>
> 		</td>
> 	</tr>
> 	</xsl:for-each>
> </table>
> </xsl:template>
> </xsl:stylesheet>
> -----------------------
> 
> this correctly produces (sorted by cat):
> -----------------------
> Person: jennifer 
> cat-felix   dog-fido   fish-moby   turtle-browny 
>   
> Person: simon 
> cat-tom   dog-scooby   fish-conroy   turtle-red 
>   
> Person: george 
> cat-zoro   dog-butch   fish-jaws   turtle-greeny 
> -----------------------
> 
> 
> but I want to sort by turtle (I want this output):
> -----------------------
> Person: jennifer 
> cat-felix   dog-fido   fish-moby   turtle-browny 
> 
> Person: george 
> cat-zoro   dog-butch   fish-jaws   turtle-greeny 
>  
> Person: simon 
> cat-tom   dog-scooby   fish-conroy   turtle-red 
> -----------------------
> 
> I changed the sort line to be:
> <xsl:sort select="turtle"/>
> but it doesn't work, my output is:
> -----------------------
> Person: george 
> cat-zoro   dog-butch   fish-jaws   turtle-greeny 
>   
> Person: jennifer 
> cat-felix   dog-fido   fish-moby   turtle-browny 
>   
> Person: simon 
> cat-tom   dog-scooby   fish-conroy   turtle-red 
> -----------------------
> 
> how can I sort on the turtle column from the document reference?
> 
> thanks for any ideas.
> will

Hi Will,

Here's a solution, which even does *not* need to produce an
intermediary output in an RTF:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/">
    <xsl:variable name="firstRoot" select="."/>
    <xsl:for-each select="document('person2.xml')/people/person">
      <xsl:sort select="turtle"/>
      person: <xsl:value-of select="@name"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="$firstRoot/people/person[@name =
current()/@name]/*">
        <xsl:value-of select="concat(., ' ')"/>
      </xsl:for-each>
      <xsl:for-each select="*">
        <xsl:value-of select="concat(., ' ')"/>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

When applied to your source xml document, the result is:


      person: jennifer
cat-felix dog-fido fish-moby turtle-browny 
      person: george
cat-zoro dog-butch fish-jaws turtle-greeny 
      person: simon
cat-tom dog-scooby fish-conroy turtle-red 



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com

 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.