XML/XSLT stylesheet/Sort

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

for each sort descending

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"?><!--results-->
  <!--match within results-->
    <!--date within match within results-->10-Jun-98
    <!--team within match within results-->Brazil
    <!--team within match within results-->Scotland
  
  <!--match within results-->
    <!--date within match within results-->10-Jun-98
    <!--team within match within results-->Morocco
    <!--team within match within results-->Norway
  
  <!--match within results-->
    <!--date within match within results-->16-Jun-98
    <!--team within match within results-->Scotland
    <!--team within match within results-->Norway



Set sort order as ascending

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>
        <h1>My sorted list of planets</h1>
        <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>
      <h1>My sorted list of planets</h1>
      
      
      
      
      description
      
      2
       12104 km (7505 miles)
       0
       482C (900F)
       243.01 earth days
       224.7 earth days
      
   </body>
</html>



sort by attribute

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>
        <h1>My sorted list of planets</h1>
        <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>
      <h1>My sorted list of planets</h1>
      
      description
      
      2
       12104 km (7505 miles)
       0
       482C (900F)
       243.01 earth days
       224.7 earth days
      
      
      
      
   </body>
</html>



Sort by attribute value

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



sort by different level of node

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>



Sort by element text

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>
        <h1>My sorted list of planets</h1>
        <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>
      <h1>My sorted list of planets</h1>
      
      
      
      
      description
      
      2
       12104 km (7505 miles)
       0
       482C (900F)
       243.01 earth days
       224.7 earth days
      
   </body>
</html>



Sort by substring

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)" />
      <!-- year  -->
      <xsl:sort select="substring(@hireDate,1,2)" />
      <!-- month -->
      <xsl:sort select="substring(@hireDate,3,2)" />
      <!-- day   -->
    </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



Sort by two columns

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



sort element by data type

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



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

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



sorts in numeric mode.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <car id="11"/>
  <car id="6"/>
  <car id="105"/>
  <car id="28"/>
  <car id="9"/>
</data>
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="/">
      <TABLE>
        <xsl:for-each select="//car">
          <xsl:sort data-type="number" select="@id"/>
          <TR>
            <TH>
              <xsl:text>Car-</xsl:text>
              <xsl:value-of select="@id"/>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TH>Car-6</TH></TR><TR><TH>Car-9</TH></TR><TR><TH>Car-11</TH></TR><TR><TH>Car-28</TH></TR><TR><TH>Car-105</TH></TR></TABLE>



sorts in text

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>    
    <car id="11"/>
  <car id="6"/>
  <car id="105"/>
  <car id="28"/>
  <car id="9"/>
</data>
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="/">
      <TABLE>
        <xsl:for-each select="//car">
          <xsl:sort data-type="text" select="@id"/>
          <TR>
            <TH>
              <xsl:text>Car-</xsl:text>
              <xsl:value-of select="@id"/>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TH>Car-105</TH></TR><TR><TH>Car-11</TH></TR><TR><TH>Car-28</TH></TR><TR><TH>Car-6</TH></TR><TR><TH>Car-9</TH></TR></TABLE>



sorts lowercase letters first

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>    
    <word id="czech"/>
  <word id="Czech"/>
  <word id="cook"/>
  <word id="TooK"/>
  <word id="took"/>
  <word id="Took"/>
</data>
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="/">
      <TABLE>
        <xsl:for-each select="//word">
          <xsl:sort case-order="lower-first" select="@id"/>
          <TR>
            <TH>
              <xsl:value-of select="@id"/>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TH>cook</TH></TR><TR><TH>czech</TH></TR><TR><TH>Czech</TH></TR><TR><TH>took</TH></TR><TR><TH>Took</TH></TR><TR><TH>TooK</TH></TR></TABLE>



sorts upercase letters first

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <word id="czech"/>
  <word id="Czech"/>
  <word id="cook"/>
  <word id="TooK"/>
  <word id="took"/>
  <word id="Took"/>
</data>
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="/">
      <TABLE>
        <xsl:for-each select="//word">
          <xsl:sort case-order="upper-first" select="@id"/>
          <TR>
            <TH>
              <xsl:value-of select="@id"/>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TH>cook</TH></TR><TR><TH>Czech</TH></TR><TR><TH>czech</TH></TR><TR><TH>TooK</TH></TR><TR><TH>Took</TH></TR><TR><TH>took</TH></TR></TABLE>



Sort value first then output

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



sort with current-grouping-key() function

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>
        <h3>header 3</h3>
        <xsl:for-each-group select="employees/employee"
          group-by="Country">
          <xsl:sort select="current-grouping-key()" />
          <paragraph>
            :
            <b>
              <xsl:value-of
                select="current-grouping-key()" />
            </b>
            <ul>
              <xsl:apply-templates
                select="current-group()">
                <xsl:sort select="LastName" />
              </xsl:apply-templates>
            </ul>
          </paragraph>
        </xsl:for-each-group>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="employee">
    <li>
      <xsl:value-of select="LastName" />
      ,
      <xsl:value-of select="FirstName" />
    </li>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <h3>header 3</h3>
      <paragraph>
                     :
                     <b>UK</b><ul>
            <li>D
                     ,
                     C
            </li>
         </ul>
      </paragraph>
      <paragraph>
                     :
                     <b>USA</b><ul>
            <li>B
                     ,
                     A
            </li>
         </ul>
      </paragraph>
   </body>
</html>