<xsl:sort> | |
現在のコンテキストの並べ替えキーを定義します。この要素は、<xsl:apply-templates> 要素または <xsl:for-each> 要素の子として使用します。これらの要素内では、最初の <xsl:sort> により第 1 並べ替えキーが定義され、2 つ目の <xsl:sort> により第 2 並べ替えキーが定義されます。 | |
カテゴリ | |
サブ命令 (<xsl:sort> は、<xsl:apply-templates> 要素または <xsl:for-each> 要素の子として指定する必要があります) |
|
必須の属性 | |
なし。 |
|
省略可能な属性 | |
|
|
コンテンツ | |
なし。 |
|
指定先 | |
<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" 属性により、データが番号順に並べ替えられます。 |