3

属性値 @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(.,'&#xA;&#xD;', '  ')" />
</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を除くすべてをソートする」のようなものになります

4

2 に答える 2

2
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>

<!-- all elements except a few sort their children -->
<xsl:template match="*[not(self::elemetsNOTToBeSorted | self::otherElemetsNOTToBeSorted)]">
    <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:apply-templates>
            <xsl:sort select="@id" />
            <xsl:sort select="@category" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<!-- ... -->

ここでは、一致式の特異性が役割を果たすことに注意してください。より具体的な一致式によって、実行するテンプレートが決まります。

  • node()はより具体的ではない*ため、要素ノードはによって処理されます<xsl:template match="*">
  • 一致self::elemetsNOTToBeSorted | self::otherElemetsNOTToBeSortedする要素は、IDテンプレートによって処理されます。
于 2012-10-11T08:44:32.237 に答える
0

この行を追加すると、elementsNOTToBeSorted のすべての子孫要素が無視されます。

<xsl:template match="elemetsNOTToBeSorted" />

これらの要素を出力ドキュメントに引き続き表示したい場合は、抑制する代わりにコピーするだけです。

<xsl:template match="elemetsNOTToBeSorted">
    <xsl:copy-of select="." />
</xsl:template>
于 2012-10-10T20:47:45.353 に答える