属性値 @id または @category に応じてドキュメント全体をソートする XSL が既にあります。今、決して並べ替えてはならないノードを定義して、それを強化したいと考えています。
サンプル XML を次に示します。
<root>
[several levels of xml open]
<elemetsToBeSorted>
<sortMe id="8" />
<sortMe id="2" />
<sortMe id="4" />
</elemetsToBeSorted>
<elemetsNOTToBeSorted>
<dontSortMe id="5" />
<dontSortMe id="3" />
<dontSortMe id="2" />
</elemetsNOTToBeSorted>
[several levels of xml closing]
</root>
これは私のXSLです:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<!-- Sort all Elements after their id or category -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()">
<xsl:sort select="@id" />
<xsl:sort select="@category" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- Next two templates clean up formatting after sorting -->
<xsl:template match="text()[not(string-length(normalize-space()))]" />
<xsl:template match="text()[string-length(normalize-space()) > 0]">
<xsl:value-of select="translate(.,'

', ' ')" />
</xsl:template>
期待される出力:
<root>
[several levels of xml open]
<elemetsToBeSorted>
<sortMe id="2" />
<sortMe id="4" />
<sortMe id="8" />
</elemetsToBeSorted>
<elemetsNOTToBeSorted>
<dontSortMe id="5" />
<dontSortMe id="3" />
<dontSortMe id="2" />
</elemetsNOTToBeSorted>
[several levels of xml closing]
</root>
XSL が "elementsNOTToBeSorted" を無視するようにするにはどうすればよいですか?
編集:ソートする必要がある要素は何百もありますが、ソートしない要素(およびその子要素)はわずかです。したがって、ロジックは「aとbを除くすべてをソートする」のようなものになります