Thẻ <xsl:sort> trong XSLT

Định nghĩa và cách sử dụng

Element <xsl:sort> được sử dụng để sắp xếp kết quả.

Chú ý:<xsl:sort> luôn luôn nằm trong nội bộ <xsl:for-each> hoặc <xsl:apply-templates>.

Cú pháp

<xsl:sort
select="expression"
lang="language-code"
data-type="text|number|qname"
order="ascending|descending"
case-order="upper-first|lower-first"/>

Thuộc tính

Thuộc tính Giá trị Mô tả
select XPath-expression Tùy chọn. Định nghĩa từ khóa sắp xếp của nút, tức là dựa trên nút hoặc tập hợp nút nào để sắp xếp.
lang language-code Tùy chọn. Định nghĩa ngôn ngữ được sử dụng để sắp xếp.
data-type
  • text
  • number
  • qname
Tùy chọn. Định nghĩa loại dữ liệu của dữ liệu được sắp xếp. Mặc định là "text".
order
  • ascending
  • descending
Tùy chọn. Định nghĩa thứ tự sắp xếp. Mặc định là "ascending".
case-order
  • upper-first
  • lower-first
Tùy chọn. Định nghĩa có sắp xếp theo thứ tự chữ cái in hoa trước hay không.

Mô hình

Ví dụ 1

Dưới đây là ví dụ để sắp xếp theo từ khóa artist:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>