xsl-list
[Top] [All Lists]

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

2002-09-16 17:40:24
Hello Will,

you either have to statically change the context to the second document

<xsl:for-each select="document('xml2.xml')/people/person">
  <xsl:sort select="turtle"/>
  ...
</xsl:for-each>

(but I think that's not an option)

or you have to create a temporary tree with all information merged. You can do this either via a 2 transformations or using an extension function exslt:node-set(). While extension functions always have the disadvantage of less portability, exslt is an approach against that matter (http://www.exslt.org).

<xsl:variable name="persons">
  <xsl:for-each select="people/person">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select="*"/>
      <xsl:variable name="ownerName" select="@name"/>
      <xsl:for-each select="document('xml2.xml')">
        <xsl:copy-of select="key('turtleByOwner', $ownerName)"
      </xsl:for-each>
      <xsl:copy-of select="
    </xsl:copy>
  </xsl:for-each>
</xsl:variable>

<xsl:for-each select="exslt:node-set($persons)/person">
  <xsl:sort select="cat"/> <!-- or turtle -->
  ...
</xsl:for-each>

Other comments to your stylesheet:

Are you sure you want version="2.0"??
In the match attribute of the key, you don't need //. The key indexes the whole XML tree (so all turtle elements, even without //).

Regards,

Joerg

Carter, Will 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


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