XML Tutorial/XSLT stylesheet/Sort

Материал из Web эксперт
Версия от 11:27, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

sort by attribute: sort select="@joined" data-type="number" order="descending"

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<member>
 <state joined="1995">Austria</state>
 <state joined="1950">Belgium</state>
 <state joined="1973">Denmark</state>
 <state joined="1986">Portugal</state>
 <state joined="1986">Spain</state>
 <state joined="1995">Sweden</state>
 <state joined="1973">United Kingdom</state>
</member>

File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />
  <xsl:output
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
  <xsl:template match="member">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>EU Member States</title>
        <style type="text/css">
          h3 {font-size: 16pt} body {font-size: 13pt}
        </style>
      </head>
      <body>
        <h3>EU Member States</h3>
        <paragraph>
          There are
          <xsl:text> </xsl:text>
          <xsl:value-of select="count(state)" />
          member states, listed starting from the most recent
          year:
        </paragraph>
        <ul>
          <xsl:apply-templates select="state">
            <xsl:sort select="@joined" data-type="number"
              order="descending" />
          </xsl:apply-templates>
        </ul>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="state">
    <xsl:element name="li"
      namespace="http://www.w3.org/1999/xhtml">
      <xsl:apply-templates />
      <xsl:text> (</xsl:text>
      <xsl:value-of select="@joined" />
      <xsl:text>)</xsl:text>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>EU Member States</title>
      <style type="text/css">
          h3 {font-size: 16pt} body {font-size: 13pt}
        </style>
   </head>
   <body>
      <h3>EU Member States</h3>
      <paragraph>
          There are
           7
          member states, listed starting from the most recent
          year:
        </paragraph>
      <ul>
         <li>Austria (1995)</li>
         <li>Sweden (1995)</li>
         <li>Portugal (1986)</li>
         <li>Spain (1986)</li>
         <li>Denmark (1973)</li>
         <li>United Kingdom (1973)</li>
         <li>Belgium (1950)</li>
      </ul>
   </body>
</html>


Sort by different data types and order

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<BOOK>
   <TITLE>Java</TITLE>
   <AUTHOR>
      <FIRSTNAME>Jack</FIRSTNAME>
      <LASTNAME>Smith</LASTNAME>
   </AUTHOR>
   <BINDING>hardcover</BINDING>
   <PAGES>724</PAGES>
   <PRICE>$9.95</PRICE>
</BOOK>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet
   version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <HTML>
      <HEAD>
         <TITLE>Book Inventory</TITLE>
      </HEAD>
      <BODY>
      <H2>Book Inventory</H2>
      <H3>Trade Paperback Books</H3>
      <xsl:apply-templates 
         select="INVENTORY/BOOK[BINDING="trade paperback"]">
         <xsl:sort 
            select="AUTHOR/LASTNAME" 
            data-type="text"
            order="ascending"/>
         <xsl:sort 
            select="AUTHOR/FIRSTNAME" 
            data-type="text"
            order="ascending"/>
         <xsl:sort
            select="PAGES" 
            data-type="number"
            order="descending"/>
      </xsl:apply-templates>
      </BODY>
      </HTML>
   </xsl:template>
   <xsl:template match="BOOK">
      <SPAN STYLE="font-style:italic">Author: </SPAN>
      <xsl:value-of select="AUTHOR"/><BR/>
      <SPAN STYLE="font-style:italic">Title: </SPAN>
      <xsl:value-of select="TITLE"/><BR/>
      <SPAN STYLE="font-style:italic">Binding type: </SPAN>
      <xsl:value-of select="BINDING"/><BR/>
      <SPAN STYLE="font-style:italic">Number of pages: </SPAN>
      <xsl:value-of select="PAGES"/><BR/>
      <SPAN STYLE="font-style:italic">Price: </SPAN>
      <xsl:value-of select="PRICE"/><P/>
    </xsl:template>
</xsl:stylesheet>
Output:
<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <TITLE>Book Inventory</TITLE>
   </HEAD>
   <BODY>
      <H2>Book Inventory</H2>
      <H3>Trade Paperback Books</H3>
   </BODY>
</HTML>


Sort by last name than first name

File: Data.xml
<?xml version="1.0"?>
<addressbook>
  <address>
    <name>
      <title>Mr.</title>
      <first-name>Jack</first-name>
      <last-name>Smith</last-name>
    </name>
    <street>1234 Main Street</street>
    <city>New York</city>
    <state>WI</state>
    <zip>48392</zip>
  </address>
</addressbook>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="addressbook/address">
      <xsl:sort select="name/last-name"/>
      <xsl:sort select="name/first-name"/>
      <xsl:if test="name/title">
        <xsl:value-of select="name/title"/>
        <xsl:text> </xsl:text>
      </xsl:if>
      <xsl:value-of select="name/first-name"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="name/last-name"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:value-of select="street"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:value-of select="city"/>
      <xsl:text>, </xsl:text>
      <xsl:value-of select="state"/>
      <xsl:text>  </xsl:text>
      <xsl:value-of select="zip"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
Mr. Jack Smith
1234 Main Street
New York, WI  48392


sort by name()

File: Data.xml
<list>
 <freezer>
  <element>peas</element>
  <element>green beans</element>
  <element>pot pie</element>
  <element>ice cream</element>
 </freezer>
 <bakery>
  <element>rolls</element>
  <element>jelly doughnuts</element>
  <element>bread</element>
 </bakery>
 <produce>
  <element>bananas</element>
  <element>kumquats</element>
  <element>apples</element>
 </produce>
</list>

File: Transform.xslt
<?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" />
  <xsl:template match="list">
    <xsl:apply-templates select="*">
      <xsl:sort select="name()" />
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="*">
    <xsl:text>Section: </xsl:text>
    <xsl:value-of select="name()" />
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates select="element">
      <xsl:sort />
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="element">
    <xsl:text> * </xsl:text>
    <xsl:apply-templates />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
Section: bakery
 * bread
 * jelly doughnuts
 * rolls
Section: freezer
 * green beans
 * ice cream
 * peas
 * pot pie
Section: produce
 * apples
 * bananas
 * kumquats


sort data-type="number" order="descending"

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<member>
 <state joined="1995">Austria</state>
 <state joined="1950">Belgium</state>
 <state joined="1973">Denmark</state>
 <state joined="1986">Portugal</state>
 <state joined="1986">Spain</state>
 <state joined="1995">Sweden</state>
 <state joined="1973">United Kingdom</state>
</member>

File: Transform.xslt
<?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" />
  <xsl:template match="member">
    <xsl:text>Number of EU Member States: </xsl:text>
    <xsl:value-of select="count(state)" />
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates select="state/@joined">
      <xsl:sort data-type="number" order="descending" />
    </xsl:apply-templates>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
  <xsl:template match="state/@joined">
    <xsl:text> - </xsl:text>
    <xsl:apply-templates select=".." />
    <xsl:text> (</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>)&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
Number of EU Member States: 7
 - Austria (1995)
 - Sweden (1995)
 - Portugal (1986)
 - Spain (1986)
 - Denmark (1973)
 - United Kingdom (1973)
 - Belgium (1950)


sort elements

File: Data.xml
<names>
    <name>John</name>
    <name>Josua</name>
    <name>Charles</name>
    <name>Alice</name>
    <name>Martha</name>
    <name>George</name>
</names>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <TABLE>
        <xsl:apply-templates select="//name">
          <xsl:sort order="descending" select="."/>
        </xsl:apply-templates>
      </TABLE>
    </xsl:template>
    <xsl:template match="name">
      <TR>
        <td>
          <xsl:value-of select="."/>
        </TH>
      </TR>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><td>Martha</TH></TR><TR><td>Josua</TH></TR><TR><td>John</TH></TR><TR><td>George</TH></TR><TR><td>Charles</TH></TR><TR><td>Alice</TH></TR></TABLE>


Sort in a for-each loop

File: Data.xml
<?xml version="1.0"?>
<numberlist>
  <number>127</number>
  <number>23</number>
  <number>10</number>
</numberlist>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="no"/>
  <xsl:strip-space elements="*"/>
  <xsl:variable name="newline">
  <xsl:text></xsl:text>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:for-each select="numberlist/number">
      <xsl:sort select="." data-type="number"/>
      <xsl:value-of select="."/>
      <xsl:value-of select="$newline"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
1023127


sort order="descending"

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<europe>
  <state>Belgium</state>
  <state>Germany</state>
  <state>Finland</state>
  <state>Greece</state>
  <state>Ireland</state>
  <state>Luxembourg</state>
  <state>Switzerland</state>
</europe>
File: Transform.xslt
<?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" />
  <xsl:template match="europe">
    <xsl:apply-templates select="state">
      <xsl:sort order="descending" />
    </xsl:apply-templates>
    <xsl:text>Number of European States: </xsl:text>
    <xsl:value-of select="count(state)" />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
  <xsl:template match="state">
    <xsl:text> - </xsl:text>
    <xsl:apply-templates />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
 - Switzerland
 - Luxembourg
 - Ireland
 - Greece
 - Germany
 - Finland
 - Belgium
Number of European States: 7


sort order="descending" select="."

File: Data.xml
<names>
    <name>John</name>
    <name>Josua</name>
    <name>Charles</name>
    <name>Alice</name>
    <name>Martha</name>
    <name>George</name>
</names>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <TABLE>
        <xsl:for-each select="//name">
          <xsl:sort order="descending" select="."/>
          <TR>
            <td>
              <xsl:value-of select="."/>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><td>Martha</TH></TR><TR><td>Josua</TH></TR><TR><td>John</TH></TR><TR><td>George</TH></TR><TR><td>Charles</TH></TR><TR><td>Alice</TH></TR></TABLE>


sort select="."

File: Data.xml

<?xml version="1.0"?>
<numberlist>
  <number>127</number>
  <number>23</number>
  <number>10</number>
</numberlist>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="numberlist/number">
      <xsl:sort select="."/>
      <xsl:value-of select="."/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
10
127
23


sort select="." data-type="number"

File: Data.xml
<?xml version="1.0"?>
<numberlist>
  <number>127</number>
  <number>23</number>
  <number>10</number>
</numberlist>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="numberlist/number">
      <xsl:sort select="." data-type="number"/>
      <xsl:value-of select="."/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
10
23
127


sort select="xs:integer(.)"

File: Data.xml
<?xml version="1.0"?>
<numberlist>
  <number>127</number>
  <number>23</number>
  <number>10</number>
</numberlist>
File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="numberlist/number">
      <xsl:sort select="xs:integer(.)"/>
      <xsl:value-of select="."/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
10
23
127


The <xsl:sort> Element is used to specify sort order for node-sets

The xsl:sort element can be used together with the xsl:apply-templates element and the xsl:for-each element. 
File: Data.xml
<names>
    <name>John</name>
    <name>Josua</name>
    <name>Charles</name>
    <name>Alice</name>
    <name>Martha</name>
    <name>George</name>
</names>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <TABLE>
        <xsl:for-each select="//name">
          <xsl:sort order="ascending" select="."/>
          <TR>
            <td>
              <xsl:value-of select="."/>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>

</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><td>Alice</TH></TR><TR><td>Charles</TH></TR><TR><td>George</TH></TR><TR><td>John</TH></TR><TR><td>Josua</TH></TR><TR><td>Martha</TH></TR></TABLE>