アイテムのリストがあります:
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
そして私は出力として欲しい
z
c
x
a
ファイルに注文情報がなく、行を逆にしたいだけです。ソース ファイルの最後の行が、出力の最初の行になります。アイテムのコンテンツでソートせずに XSLT を使用してこの問題を解決するにはどうすればよいですか?
2つのXSLTソリューションを紹介します。
I.再帰を使用したXSLT1.0 このソリューションは、ノードが兄弟である場合だけでなく、すべてのノードセットで機能することに注意してください。
この変換:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:call-template name="reverse">
<xsl:with-param name="pList" select="*"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="pList"/>
<xsl:if test="$pList">
<xsl:value-of
select="concat($pList[last()], '
')"/>
<xsl:call-template name="reverse">
<xsl:with-param name="pList"
select="$pList[not(position() = last())]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
このXMLドキュメントに適用した場合:
<t>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</t>
必要な結果を生成します:
z
c
x
a
II。XSLT 2.0ソリューション:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:value-of select="reverse(*)/string(.)"
separator="
"/>
</xsl:template>
</xsl:stylesheet>
この変換を同じXMLドキュメントに適用すると、同じ正しい結果が生成されます。
XMLコード:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<device>
<element>a</element>
<element>x</element>
<element>c</element>
<element>z</element>
</device>
XSLTコード:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//device">
<xsl:for-each select="element">
<xsl:sort select="position()" data-type="number" order="descending"/>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
注: data-type = "number"を使用していて、いずれかの値が数値でない場合、これらの非数値は数値の前に並べ替えられます。つまり、order = "ascending"を使用している場合は、数値以外の値が最初に表示されます。order = "descending"を使用すると、数値以外の値が最後に表示されます。
非数値はソートされていないことに注意してください。それらは、検出された順序で出力ドキュメントに表示されるだけです。
また、これを読むと便利な場合があります。
完全な XML がどのように見えるかわからないので、適切な形式にするために<doc>
要素をラップしました。
<doc>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</doc>
このスタイルシートに対してサンプル XML を実行します。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:call-template name="reverse">
<xsl:with-param name="item" select="doc/item[position()=last()]" />
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="item" />
<xsl:value-of select="$item" />
<!--Adds a line feed-->
<xsl:text> </xsl:text>
<!--Move on to the next item, if we aren't at the first-->
<xsl:if test="$item/preceding-sibling::item">
<xsl:call-template name="reverse">
<xsl:with-param name="item" select="$item/preceding-sibling::item[1]" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
要求された出力を生成します。
z
c
x
a
実際の XML に一致するように xpath を調整する必要がある場合があります。
次の XML 入力を検討してください。
<?xml version="1.0" encoding="utf-8" ?>
<items>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</items>
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="text" />
<xsl:template match="/items[1]">
<xsl:variable name="items-list" select="." />
<xsl:variable name="items-count" select="count($items-list/*)" />
<xsl:for-each select="item">
<xsl:variable name="index" select="$items-count+1 - position()"/>
<xsl:value-of select="$items-list/item[$index]"/>
<xsl:value-of select="'
'"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
そして結果:
z
c
x
a