<xsl:sort>  
現在のコンテキストの並べ替えキーを定義します。この要素は、<xsl:apply-templates> 要素または <xsl:for-each> 要素の子として使用します。これらの要素内では、最初の <xsl:sort> により第 1 並べ替えキーが定義され、2 つ目の <xsl:sort> により第 2 並べ替えキーが定義されます。
 
カテゴリ

サブ命令 (<xsl:sort> は、<xsl:apply-templates> 要素または <xsl:for-each> 要素の子として指定する必要があります)

 
必須の属性

なし。

 
省略可能な属性
select
並べ替えるノードを定義する XPath 式。

lang
並べ替えに使用する言語を定義する文字列。言語コードは RFC1766 で定義されています (http://www.ietf.org/rfc/rfc1766.txt を参照)。

data-type
並べ替える項目の種類を定義する属性。使用可能な値は、 number text です。初期設定値は text です。XSLT プロセッサには、他の値もサポートするオプションがあります。値 32 10 120 data-type= " text " で並べ替えると、 10 120 32 が返され、 data-type= " number " の場合は 10 32 120 が返されます。

order
並べ替えの順序を定義する属性。使用可能な値は、 ascending descending です。

case-order
大文字と小文字を並べ替える順序を定義する属性。使用可能な値は、 upper-first lower-first です。

 
コンテンツ

なし。

 
指定先

<xsl:apply-templates><xsl:for-each>

 
定義先

XSLT 10 節「並べ替え」

 

<xsl:sort> を次のスタイルシートに示します。

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

  <xsl:output method="text"/>

  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:call-template name="ascending-alpha-sort">
      <xsl:with-param name="items" select="/sample/textlist/listitem"/>
    </xsl:call-template>
    <xsl:call-template name="ascending-alpha-sort">
      <xsl:with-param name="items" select="/sample/numericlist/listitem"/>
    </xsl:call-template>
    <xsl:call-template name="ascending-numeric-sort">
      <xsl:with-param name="items" select="/sample/numericlist/listitem"/>
    </xsl:call-template>
    <xsl:call-template name="descending-alpha-sort">
      <xsl:with-param name="items" select="/sample/textlist/listitem"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="ascending-alpha-sort">
    <xsl:param name="items"/>
    <xsl:text>Ascending text sort:</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="$items">
      <xsl:sort select="."/>
      <xsl:value-of select="."/>
      <xsl:value-of select="$newline"/>
    </xsl:for-each>
    <xsl:value-of select="$newline"/>
  </xsl:template>

  <xsl:template name="descending-alpha-sort">
    <xsl:param name="items"/>
    <xsl:text>Descending text sort:</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="$items">
      <xsl:sort select="." order="descending"/>
      <xsl:value-of select="."/>
      <xsl:value-of select="$newline"/>
    </xsl:for-each>
    <xsl:value-of select="$newline"/>
  </xsl:template>

  <xsl:template name="ascending-numeric-sort">
    <xsl:param name="items"/>
    <xsl:text>Ascending numeric sort:</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="$items">
      <xsl:sort select="." data-type="number"/>
      <xsl:value-of select="."/>
      <xsl:value-of select="$newline"/>
    </xsl:for-each>
    <xsl:value-of select="$newline"/>
  </xsl:template>

</xsl:stylesheet>

このスタイルシートでは、名前が指定された 3 つのテンプレートを定義します。各テンプレートは、異なる順序または異なる data-type<listitem> を並べ替えます。このスタイルシートを次のドキュメントに対して使用します。

<?xml version="1.0"?>
<sample>
  <numericlist>
    <listitem>1</listitem>
    <listitem>3</listitem>
    <listitem>23</listitem>
    <listitem>120</listitem>
    <listitem>2</listitem>
  </numericlist>
  <textlist>
    <listitem>3</listitem>
    <listitem>apple</listitem>
    <listitem>orange</listitem>
    <listitem>dragonfruit</listitem>
    <listitem>carambola</listitem>
  </textlist>
</sample>

結果は次のとおりです。


Ascending text sort:
3
apple
carambola
dragonfruit
orange

Ascending text sort:
1
120
2
23
3

Ascending numeric sort:
1
2
3
23
120

Descending text sort:
orange
dragonfruit
carambola
apple
3

data-type="numeric" 属性により、データが番号順に並べ替えられます。