You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSL同一层级不同节点排序的层级冲突问题求助

排序XML节点时,祖先节点的排序规则导致子节点未按预期排序

我有一份XML文档,希望依据不同规则对不同节点进行排序,输出仍为XML格式。目前遇到的问题是:当匹配某一<xsl:sort>规则的节点集是另一<xsl:sort>规则所捕获节点的祖先时,部分节点未按预期排序。

使用的XSL代码

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="//*[string]">
        <xsl:copy>
            <xsl:for-each select="string">
                <xsl:sort order="ascending" data-type="text" case-order="upper-first"/>
                <xsl:copy-of select="."/>
                <xsl:value-of select="'&#10;'"/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/root/foo">
        <xsl:copy>
            <xsl:for-each select="bar">
                <xsl:sort order="ascending" data-type="text" case-order="upper-first" select="name"/>
                <xsl:copy-of select="."/>
                <xsl:value-of select="'&#10;'"/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

示例XML输入

<root>
    <foo>
        <bar>
            <name>W</name>
            <blah>
                <string>P</string>
                <string>R</string>
                <string>Q</string>
                <string>S</string>
            </blah>
        </bar>
        <bar>
            <name>U</name>
            <blah>
                <string>N</string>
                <string>L</string>
                <string>M</string>
                <string>O</string>
            </blah>
        </bar>
        <bar>
            <name>V</name>
            <blah>
                <string>Z</string>
                <string>X</string>
                <string>Y</string>
            </blah>
        </bar>
    </foo>
    <some>
        <other>
            <hierarchy>
                <string>D</string>
                <string>A</string>
                <string>C</string>
                <string>B</string>
            </hierarchy>
        </other>
    </some>
    <some>
        <other>
            <hierarchy>
                <with />
                <no />
                <impact />
            </hierarchy>
        </other>
    </some>
</root>

当前输出结果

<root>
    <foo><bar>
            <name>U</name>
            <blah>
                <string>N</string>
                <string>L</string>
                <string>M</string>
                <string>O</string>
            </blah>
        </bar>
<bar>
            <name>V</name>
            <blah>
                <string>Z</string>
                <string>X</string>
                <string>Y</string>
            </blah>
        </bar>
<bar>
            <name>W</name>
            <blah>
                <string>P</string>
                <string>R</string>
                <string>Q</string>
                <string>S</string>
            </blah>
        </bar>
</foo>
    <some>
        <other>
            <hierarchy><string>A</string>
<string>B</string>
<string>C</string>
<string>D</string>
</hierarchy>
        </other>
    </some>
    <some>
        <other>
            <hierarchy>
                <with/>
                <no/>
                <impact/>
            </hierarchy>
        </other>
    </some>
</root>

可以看到,<name>节点以及some-other-hierarchy路径下的<string>节点已完成排序,但foo-bar-blah路径下的<string>节点未被排序。

必须使用以下版本的库:

  • libxml 21200
  • libxslt 10138
  • libexslt 821

尽管这些版本较旧,但我认为问题并非源于版本限制,而是我对<xsl:sort>的使用存在认知不足,恳请提供解决方案。

内容的提问来源于stack exchange,提问作者Happy Green Kid Naps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 12:41:02