xsl:sort

only for RuBoard

xsl: sort

xsl:sort specifies sort criteria for a node list selected by xsl:value-of or

xsl:apply-templates .

Attributes

Attribute Name

Enforced

Description

Values

select

Optional

The sort key for the node. If not specified, uses the value of the current node ( . ) as the sort key.

A string value, usually an XPath expression.

data-type

Optional

Specifies the data type for the strings.

text , number , or a QName that's expanded into a dt-expanded-name . The expanded-name identifies the data type.

order

Optional

Specifies the sort order.

ascending or descending

lang

Optional

Specifies which language's alphabet is used to determine sort order.

NMToken . If unspecified, the language is determined from the system's environment.

case-order

Optional

Determines if strings will be sorted with uppercase first or lowercase first.

upper-first or lower-first . The default is lower- first (despite the documentation in MSDN to the contrary).

Example

Consider the following XML document, test.xml :

 <?xml version="1.0" encoding="utf-8" ?>  <links>       <link name="C" id="1" />       <link name="B" id="2" />       <link name="A" id="11" />       <link name="b" id="22" />       <link name="a" id="12" />       <link name="c" id="23" />  </links> 

The following stylesheet examples have a single template rule that matches the root node. They iterate through the link elements, which are sorted. The only difference is their xsl:sort element's parameters and the output generated:

 <?xml version="1.0" encoding="UTF-8" ?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">       <xsl:output method="text" omit-xml-declaration="yes" />       <xsl:template match="/">            <xsl:for-each select="links/link">                 <xsl:sort select="attribute::name" order="descending" graphics/ccc.gif case-order="lower-first" />                 <xsl:value-of select="@name" />                 <xsl:if test="position() != last()">                      <xsl:text>,</xsl:text>                 </xsl:if>            </xsl:for-each>       </xsl:template>  </xsl:stylesheet> 

The preceding stylesheet yields the following:

 C,c,B,b,A,a 

Notice that we specified the case-order attribute as lower-first . The uppercase letters are rendered first in the example. This is because the document is first sorted using the case-order and then the sort order is applied. To see this more clearly, sort in ascending order and change the previous stylesheet rule's xsl:sort element to the following declaration:

 <xsl:sort select="attribute::name" case-order="upper-first" /> 

This yields the following:

 A,a,B,b,C,c 

Changing the line of text to use all the defaults looks like this:

 <xsl:sort select="attribute::name" /> 

The default behavior yields the following.

 A,a,B,b,C,c 

The preceding examples are sorted based on text. The following examples show sorting with numeric data. We are now sorting based on the id attribute:

 <?xml version="1.0" encoding="UTF-8" ?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">       <xsl:output method="text" omit-xml-declaration="yes" />       <xsl:template match="/">            <xsl:for-each select="links/link">                 <xsl:sort select="attribute::id" />                 <xsl:value-of select="@id" />                 <xsl:if test="position() != last()">                  <xsl:text>,</xsl:text>                 </xsl:if>            </xsl:for-each>       </xsl:template>  </xsl:stylesheet> 

This stylesheet yields the following:

 1,11,12,2,22,23 

Instead of using the default data type, text, you can specify the data-type attribute as number :

 <xsl:sort select="attribute::id" data-type="number"/> 

This yields a numeric sorting instead of character sorting:

 1,2,11,12,22,23 

Parent Elements

 xsl:apply-templates, xsl:for-each 

Child Elements

None.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net