This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Joerg Heinicke <joerg dot heinicke at gmx dot de> wrote:

> 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.

It is possible without re-writing code and without creating an
intermediate RTF:

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

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

When applied to the original source xml:

persons1.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>

and the file

persons2.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>

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 

Note that the name of the child on which to sort is specified as a
xsl:param and in this example it is 'turtle'.

When I simply change this to 'cat', the result now is:


      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 




=====
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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]