XML/XSLT stylesheet/Sort

Материал из Web эксперт
Перейти к: навигация, поиск

for each sort descending

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <results group="A">

 <match>
   <date>10-Jun-98</date>
   <team score="2">Brazil</team>
   <team score="1">Scotland</team>
 </match>
 <match>
   <date>10-Jun-98</date>
   <team score="2">Morocco</team>
   <team score="2">Norway</team>
 </match>
 <match>
   <date>16-Jun-98</date>
   <team score="1">Scotland</team>
   <team score="1">Norway</team>
 </match>

</results> File: Transform.xslt <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="*">
   <xsl:comment>
     <xsl:value-of select="name()" />
     <xsl:for-each select="ancestor::*">
       <xsl:sort select="position()" order="descending" />
       <xsl:text> within </xsl:text>
       <xsl:value-of select="name()" />
     </xsl:for-each>
   </xsl:comment>
   <xsl:apply-templates />
 </xsl:template>

</xsl:transform> Output: <?xml version="1.0" encoding="UTF-8"?>

   10-Jun-98
   Brazil
   Scotland
 
 
   10-Jun-98
   Morocco
   Norway
 
 
   16-Jun-98
   Scotland
   Norway
 
</source>
   
  


Set sort order as ascending

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt" ?> <neighbours>

 <planet name="Venus">
   <description>
   description
   </description>
   <positionFromSun>2</positionFromSun>
   <diameter> 12104 km (7505 miles)</diameter>
   <moons> 0</moons>
   <meanTemp> 482C (900F)</meanTemp>
   <oneDay> 243.01 earth days</oneDay>
   <oneYear> 224.7 earth days</oneYear>
 </planet>

</neighbours> 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:import href="planetsToXHTML.xsl"/>
 <xsl:output method="html" version="4.0" indent="yes"/>
 <xsl:template match="neighbours">
   <html>
     <head>
       <title>Sorted planets</title>
     </head>
     <body>

My sorted list of planets

       <xsl:apply-templates>
         <xsl:sort select="substring-before(meanTemp/text(), "C")" order="ascending" data-type="number"/>
       </xsl:apply-templates>
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Sorted planets</title>
     </head>
  <body>

My sorted list of planets



     description
     
     2
      12104 km (7505 miles)
      0
      482C (900F)
      243.01 earth days
      224.7 earth days
     
  </body>

</html>

</source>
   
  


sort by attribute

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt" ?> <neighbours>

 <planet name="Venus">
   <description>
   description
   </description>
   <positionFromSun>2</positionFromSun>
   <diameter> 12104 km (7505 miles)</diameter>
   <moons> 0</moons>
   <meanTemp> 482C (900F)</meanTemp>
   <oneDay> 243.01 earth days</oneDay>
   <oneYear> 224.7 earth days</oneYear>
 </planet>

</neighbours> 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="html" version="4.0" indent="yes"/>
 <xsl:template match="neighbours">
   <html>
     <head>
       <title>Sorted planets</title>
     </head>
     <body>

My sorted list of planets

       <xsl:apply-templates>
         <xsl:sort select="@name" order="descending"/>
       </xsl:apply-templates>
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Sorted planets</title>
     </head>
  <body>

My sorted list of planets

     description
     
     2
      12104 km (7505 miles)
      0
      482C (900F)
      243.01 earth days
      224.7 earth days
     
     
     
     
  </body>

</html>

</source>
   
  


Sort by attribute value

   <source lang="xml">

File: Data.xml

<employees>

 <employee hireDate="04/23/1999">
   <last>A</last>
   <first>B</first>
   <salary>100000</salary>
 </employee>
 <employee hireDate="09/01/1998">
   <last>C</last>
   <first>D</first>
   <salary>95000</salary>
 </employee>
 <employee hireDate="08/20/2000">
   <last>E</last>
   <first>F</first>
   <salary>89000</salary>
 </employee>

</employees> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="employees">
   <xsl:apply-templates>
     <xsl:sort select="@hireDate" />
   </xsl:apply-templates>
 </xsl:template>
 <xsl:template match="employee">
   Last:
   <xsl:apply-templates select="last" />
   First:
   <xsl:apply-templates select="first" />
   Salary:
   <xsl:apply-templates select="salary" />
   Hire Date:
   <xsl:apply-templates select="@hireDate" />
 </xsl:template>

</xsl:stylesheet> Output:



   Last:
   A
   First:
   B
   Salary:
   100000
   Hire Date:
   04/23/1999
   Last:
   E
   First:
   F
   Salary:
   89000
   Hire Date:
   08/20/2000
   Last:
   C
   First:
   D
   Salary:
   95000
   Hire Date:
   09/01/1998
</source>
   
  


sort by different level of node

   <source lang="xml">

File: Data.xml <winelist>

 <wine grape="Chardonnay">
   <winery>shop 1</winery>
   <product>product 1</product>
   <year>1998</year>
   <prices>
     <list>6.99</list>
     <discounted>5.99</discounted>
     <case>71.50</case>
   </prices>
 </wine>
 <wine grape="Chardonnay">
   <winery>shop 2</winery>
   <product>product 2</product>
   <year>1997</year>
   <prices>
     <list>10.99</list>
     <discounted>9.50</discounted>
     <case>114.00</case>
   </prices>
 </wine>
 <wine grape="Cabernet">
   <winery>shop 1</winery>
   <product>product 1</product>
   <year>1996</year>
   <prices>
     <list>13.99</list>
     <discounted>11.99</discounted>
     <case>143.50</case>
   </prices>
 </wine>

</winelist> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="winelist">
   <xsl:copy>
     <xsl:apply-templates>
       <xsl:sort data-type="number" select="prices/discounted" />
     </xsl:apply-templates>
   </xsl:copy>
 </xsl:template>
 <xsl:template match="*">
   <xsl:copy>
     <xsl:apply-templates />
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><winelist>


<wine>

   <winery>shop 1</winery>
   <product>product 1</product>
   <year>1998</year>
   <prices>
     <list>6.99</list>
     <discounted>5.99</discounted>
     <case>71.50</case>
   </prices>
 </wine><wine>
   <winery>shop 2</winery>
   <product>product 2</product>
   <year>1997</year>
   <prices>
     <list>10.99</list>
     <discounted>9.50</discounted>
     <case>114.00</case>
   </prices>
 </wine><wine>
   <winery>shop 1</winery>
   <product>product 1</product>
   <year>1996</year>
   <prices>
     <list>13.99</list>
     <discounted>11.99</discounted>
     <case>143.50</case>
   </prices>
 </wine></winelist>
</source>
   
  


Sort by element text

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt" ?> <neighbours>

 <planet name="Venus">
   <description>
   description
   </description>
   <positionFromSun>2</positionFromSun>
   <diameter> 12104 km (7505 miles)</diameter>
   <moons> 0</moons>
   <meanTemp> 482C (900F)</meanTemp>
   <oneDay> 243.01 earth days</oneDay>
   <oneYear> 224.7 earth days</oneYear>
 </planet>

</neighbours> 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="html" version="4.0" indent="yes"/>
 <xsl:template match="neighbours">
   <html>
     <head>
       <title>Sorted planets</title>
     </head>
     <body>

My sorted list of planets

       <xsl:apply-templates>
         <xsl:sort select="positionFromSun/text()" order="ascending"/>
       </xsl:apply-templates>
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Sorted planets</title>
     </head>
  <body>

My sorted list of planets



     description
     
     2
      12104 km (7505 miles)
      0
      482C (900F)
      243.01 earth days
      224.7 earth days
     
  </body>

</html>

</source>
   
  


Sort by substring

   <source lang="xml">

File: Data.xml

<employees>

 <employee hireDate="04/23/1999">
   <last>A</last>
   <first>B</first>
   <salary>100000</salary>
 </employee>
 <employee hireDate="09/01/1998">
   <last>C</last>
   <first>D</first>
   <salary>95000</salary>
 </employee>
 <employee hireDate="08/20/2000">
   <last>E</last>
   <first>F</first>
   <salary>89000</salary>
 </employee>

</employees> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="employees">
   <xsl:apply-templates>
     <xsl:sort select="substring(@hireDate,7,4)" />
     
     <xsl:sort select="substring(@hireDate,1,2)" />
     
     <xsl:sort select="substring(@hireDate,3,2)" />
     
   </xsl:apply-templates>
 </xsl:template>
 
 <xsl:template match="employee">
   Last:
   <xsl:apply-templates select="last" />
   First:
   <xsl:apply-templates select="first" />
   Salary:
   <xsl:apply-templates select="salary" />
   Hire Date:
   <xsl:apply-templates select="@hireDate" />
 </xsl:template>

</xsl:stylesheet> Output:



   Last:
   C
   First:
   D
   Salary:
   95000
   Hire Date:
   09/01/1998
   Last:
   A
   First:
   B
   Salary:
   100000
   Hire Date:
   04/23/1999
   Last:
   E
   First:
   F
   Salary:
   89000
   Hire Date:
   08/20/2000
</source>
   
  


Sort by two columns

   <source lang="xml">

File: Data.xml

<employees>

 <employee hireDate="04/23/1999">
   <last>A</last>
   <first>B</first>
   <salary>100000</salary>
 </employee>
 <employee hireDate="09/01/1998">
   <last>C</last>
   <first>D</first>
   <salary>95000</salary>
 </employee>
 <employee hireDate="08/20/2000">
   <last>E</last>
   <first>F</first>
   <salary>89000</salary>
 </employee>

</employees> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="employees">
   <xsl:apply-templates>
     <xsl:sort select="last" />
     <xsl:sort select="first" />
   </xsl:apply-templates>
 </xsl:template>
 <xsl:template match="employee">
   Last:
   <xsl:apply-templates select="last" />
   First:
   <xsl:apply-templates select="first" />
   Salary:
   <xsl:apply-templates select="salary" />
   Hire Date:
   <xsl:apply-templates select="@hireDate" />
 </xsl:template>

</xsl:stylesheet> Output:



   Last:
   A
   First:
   B
   Salary:
   100000
   Hire Date:
   04/23/1999
   Last:
   C
   First:
   D
   Salary:
   95000
   Hire Date:
   09/01/1998
   Last:
   E
   First:
   F
   Salary:
   89000
   Hire Date:
   08/20/2000
</source>
   
  


sort element by data type

   <source lang="xml">

File: Data.xml <employees>

 <employee hireDate="04/23/1999">
   <last>A</last>
   <first>B</first>
   <salary>100000</salary>
 </employee>
 <employee hireDate="09/01/1998">
   <last>C</last>
   <first>D</first>
   <salary>95000</salary>
 </employee>
 <employee hireDate="08/20/2000">
   <last>E</last>
   <first>F</first>
   <salary>89000</salary>
 </employee>

</employees>

File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 <xsl:template match="employees">
   <xsl:for-each select="employee">
     <xsl:sort select="salary" data-type="number" />
     <xsl:if test="position() = 1">
       Lowest salary:
       <xsl:apply-templates />
     </xsl:if>
     <xsl:if test="position() = last()">
       Highest salary:
       <xsl:apply-templates />
     </xsl:if>
   </xsl:for-each>
 </xsl:template>
 

</xsl:stylesheet> Output:

       Lowest salary:
       
   E
   F
   89000
 
       Highest salary:
       
   A
   B
   100000
 
</source>
   
  


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

   <source lang="xml">

File: Data.xml <employees>

 <employee hireDate="04/23/1999">
   <last>A</last>
   <first>B</first>
   <salary>100000</salary>
 </employee>
 <employee hireDate="09/01/1998">
   <last>C</last>
   <first>D</first>
   <salary>95000</salary>
 </employee>
 <employee hireDate="08/20/2000">
   <last>E</last>
   <first>F</first>
   <salary>89000</salary>
 </employee>

</employees> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="employees">
   <xsl:apply-templates>
     <xsl:sort select="salary" data-type="number" order="descending" />
   </xsl:apply-templates>
 </xsl:template>
 <xsl:template match="employee">
   Last:
   <xsl:apply-templates select="last" />
   First:
   <xsl:apply-templates select="first" />
   Salary:
   <xsl:apply-templates select="salary" />
   Hire Date:
   <xsl:apply-templates select="@hireDate" />
   <xsl:text>
       </xsl:text>
 </xsl:template>

</xsl:stylesheet> Output:

   Last:
   A
   First:
   B
   Salary:
   100000
   Hire Date:
   04/23/1999
       
   Last:
   C
   First:
   D
   Salary:
   95000
   Hire Date:
   09/01/1998
       
   Last:
   E
   First:
   F
   Salary:
   89000
   Hire Date:
   08/20/2000
       
 
 
 
</source>
   
  


sorts in numeric mode.

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

 <car id="11"/>
 <car id="6"/>
 <car id="105"/>
 <car id="28"/>
 <car id="9"/>

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:template match="/">
<xsl:for-each select="//car"> <xsl:sort data-type="number" select="@id"/> </xsl:for-each>
             <xsl:text>Car-</xsl:text>
             <xsl:value-of select="@id"/>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
Car-6
Car-9
Car-11
Car-28
Car-105
</source>
   
  


sorts in text

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <car id="11"/>
 <car id="6"/>
 <car id="105"/>
 <car id="28"/>
 <car id="9"/>

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:template match="/">
<xsl:for-each select="//car"> <xsl:sort data-type="text" select="@id"/> </xsl:for-each>
             <xsl:text>Car-</xsl:text>
             <xsl:value-of select="@id"/>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
Car-105
Car-11
Car-28
Car-6
Car-9
</source>
   
  


sorts lowercase letters first

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <word id="czech"/>
 <word id="Czech"/>
 <word id="cook"/>
 <word id="TooK"/>
 <word id="took"/>
 <word id="Took"/>

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:template match="/">
<xsl:for-each select="//word"> <xsl:sort case-order="lower-first" select="@id"/> </xsl:for-each>
             <xsl:value-of select="@id"/>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
cook
czech
Czech
took
Took
TooK
</source>
   
  


sorts upercase letters first

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

 <word id="czech"/>
 <word id="Czech"/>
 <word id="cook"/>
 <word id="TooK"/>
 <word id="took"/>
 <word id="Took"/>

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:template match="/">
<xsl:for-each select="//word"> <xsl:sort case-order="upper-first" select="@id"/> </xsl:for-each>
             <xsl:value-of select="@id"/>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
cook
Czech
czech
TooK
Took
took
</source>
   
  


Sort value first then output

   <source lang="xml">

File: Data.xml

<employees>

 <employee hireDate="04/23/1999">
   <last>A</last>
   <first>B</first>
   <salary>100000</salary>
 </employee>
 <employee hireDate="09/01/1998">
   <last>C</last>
   <first>D</first>
   <salary>95000</salary>
 </employee>
 <employee hireDate="08/20/2000">
   <last>E</last>
   <first>F</first>
   <salary>89000</salary>
 </employee>

</employees> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="employees">
   <xsl:apply-templates>
     <xsl:sort select="salary" data-type="number" />
   </xsl:apply-templates>
 </xsl:template>
 
 <xsl:template match="employee">
   Last:
   <xsl:apply-templates select="last" />
   First:
   <xsl:apply-templates select="first" />
   Salary:
   <xsl:apply-templates select="salary" />
   Hire Date:
   <xsl:apply-templates select="@hireDate" />
   <xsl:text></xsl:text>
 </xsl:template>

</xsl:stylesheet> Output:



   Last:
   E
   First:
   F
   Salary:
   89000
   Hire Date:
   08/20/2000
   Last:
   C
   First:
   D
   Salary:
   95000
   Hire Date:
   09/01/1998
   Last:
   A
   First:
   B
   Salary:
   100000
   Hire Date:
   04/23/1999
</source>
   
  


sort with current-grouping-key() function

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <employees>

 <employee>
   <FirstName>A</FirstName>
   <LastName>B</LastName>
   <Country>USA</Country>
 </employee>
 <employee>
   <FirstName>C</FirstName>
   <LastName>D</LastName>
   <Country>UK</Country>
 </employee>

</employees>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
   <html>
     <body>

header 3

       <xsl:for-each-group select="employees/employee"
         group-by="Country">
         <xsl:sort select="current-grouping-key()" />
         <paragraph>
           :
           
             <xsl:value-of
               select="current-grouping-key()" />
           
    <xsl:apply-templates select="current-group()"> <xsl:sort select="LastName" /> </xsl:apply-templates>
         </paragraph>
       </xsl:for-each-group>
     </body>
   </html>
 </xsl:template>
 <xsl:template match="employee">
  • <xsl:value-of select="LastName" /> , <xsl:value-of select="FirstName" />
  •  </xsl:template>
    

    </xsl:stylesheet> Output: <html>

      <body>
    

    header 3

         <paragraph>
                        :
    
    UK
    • D , C
         </paragraph>
         <paragraph>
                        :
    
    USA
    • B , A
         </paragraph>
      </body>
    

    </html>

    </source>