xsl-list
[Top] [All Lists]

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

2002-09-17 00:49:49
Yes, this is exactly what I meant by swithcing the context to document 2, but I think it can not really be suggested, because you can not switch back to the cat without rewriting the code. Furthermore <xsl:sort select="*[name() = $sortparam]"/> is not possible.

Regards,

Joerg

Dimitre Novatchev wrote:
--- "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[(_at_)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.

--

System Development
VIRBUS AG
Fon  +49(0)341-979-7419
Fax  +49(0)341-979-7409
joerg(_dot_)heinicke(_at_)virbus(_dot_)de
www.virbus.de


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