[Home] [By Thread] [By Date] [Recent Entries]
Here's how one can use the FXSL function
f:xsltSort() This transformation: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="http://fxsl.sf.net/" exclude-result-prefixes="xs f"
<!-- To be applied on testFunc-xsltSort2.xml --> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/">
<employees>
<xsl:sequence select=
"f:xsltSort(/*/employee,
f:map(f:flip(f:element()), /*/sort)
)"/>
</employees>
</xsl:template>
</xsl:stylesheet>when applied on this source xml document: <employees> <employee hireDate="04/23/1999"> <last>Hill</last> <first>Phil</first> <salary>089000</salary> </employee> <employee hireDate="09/01/1998"> <last>Herbert</last> <first>Johnny</first> <salary>095000</salary> </employee> <employee hireDate="08/20/2000"> <last>Hill</last> <first>Graham</first> <salary>100000</salary> </employee> <sort order="1">last</sort> <sort order="3">salary</sort> <sort order="3">first</sort> </employees> produces the wanted result: <employees>
<employee hireDate="09/01/1998">
<last>Herbert</last>
<first>Johnny</first>
<salary>095000</salary>
</employee>
<employee hireDate="04/23/1999">
<last>Hill</last>
<first>Phil</first>
<salary>089000</salary>
</employee>
<employee hireDate="08/20/2000">
<last>Hill</last>
<first>Graham</first>
<salary>100000</salary>
</employee>
</employees>Here is the code of the function f:xsltSort(): <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="http://fxsl.sf.net/" exclude-result-prefixes="xs f"
<!--
XSLT functions:
xsltSort()
--><xsl:function name="f:xsltSort" as="item()*"> <xsl:param name="pSeq" as="item()*"/> <xsl:param name="pCriteria" as="node()*"/> <xsl:perform-sort select="$pSeq">
<xsl:sort select=
"string-join(
f:map(f:flip(f:map(), .), $pCriteria)
,
''
)
"
/>
</xsl:perform-sort>
</xsl:function></xsl:stylesheet> The $pSeq argument contains the sequence of items to be sorted. The $pCriteria argument is a sequence of functions (template references), each of which produces a (xs:string) part of the sort key, when this (sort-key-part-generating)function is applied to the current item of the sequence $pSeq . Then all such sort-key parts are string-join()-ed to produce the complete sort key of the current item of the sequence $pSeq being sorted. Because their results are concatenated from left to right, a function, which is specified to the left of another function in the sequence $pCriteria has higher priority in determining the sort order of any two items of $pSeq. The function f:element() used in the test code is an accessor function. f:element($pNode, $someStrName) produces the sequence: $pNode/*[name() = $someStrName] All functions described can be downloaded from the latest FXSL CVS.
On 3/28/07, Angela Williams <Angela.Williams@xxxxxxxxxxxxxxxxxx> wrote: Aha!
|

Cart



