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

求助:基于XML中Sort Order字段的XSL排序失效问题

基于自定义字段“Sort Order”排序XML测试用例失败的解决方法

从外部获取了一份XML文件,通过VBA导入并生成Word文档的流程均正常,但无论使用XSL还是其他方法,都无法基于名为“Sort Order”的自定义字段对测试用例进行排序。尝试多种方案均未奏效,以下是XML示例及尝试使用的XSL代码:

XML示例代码

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project>
    <projectId>10010</projectId>
    <projectKey>NBSVV</projectKey>
    <exportDate>2023-06-08 13:50:44 UTC</exportDate>
    <folders>
        <folder fullPath="Folderpath" index="2"/>
    </folders>
    <testCases>
        <testCase id="51024947" key="NBSVV-T88">
            <attachments/>
            <confluencePageLinks/>
            <createdBy>bill</createdBy>
            <createdOn>2022-10-31 03:10:46 UTC</createdOn>
            <customFields>
                <customField name="Sort Order" type="NUMBER">
                    <value><![CDATA[15]]></value>
                </customField>
                <customField name="Subsystem" type="SINGLE_CHOICE_SELECT_LIST">
                    <value><![CDATA[Interlocks]]></value>
                </customField>
            </customFields>
            <name><![CDATA[Interlocks - HEBL Vacuum Pressures High]]></name>
        </testCase>
        <testCase id="51024970" key="NBSVV-T89">
            <attachments/>
            <confluencePageLinks/>
            <createdBy>bob</createdBy>
            <createdOn>2022-10-31 03:15:07 UTC</createdOn>
            <customFields>
                <customField name="Sort Order" type="NUMBER">
                    <value><![CDATA[40]]></value>
                </customField>
                <customField name="Subsystem" type="SINGLE_CHOICE_SELECT_LIST">
                    <value><![CDATA[Interlocks]]></value>
                </customField>
            </customFields>
            <name><![CDATA[Interlocks - Tandem Vacuum Pressures Out of Tolerance]]></name>
        </testCase>
        <testCase id="51024975" key="NBSVV-T90">
            <attachments/>
            <confluencePageLinks/>
            <createdBy>jane</createdBy>
            <createdOn>2022-10-31 03:16:14 UTC</createdOn>
            <customFields>
                <customField name="Sort Order" type="NUMBER">
                    <value><![CDATA[30]]></value>
                </customField>
                <customField name="Subsystem" type="SINGLE_CHOICE_SELECT_LIST">
                    <value><![CDATA[Interlocks]]></value>
                </customField>
            </customFields>
            <name><![CDATA[Interlocks - Target Vacuum Pressures High]]></name>
        </testCase>
        <testCase id="86486160" key="NBSVV-T52">
            <attachments/>
            <confluencePageLinks/>
            <createdBy>joe</createdBy>
            <createdOn>2023-05-11 19:14:20 UTC</createdOn>
            <customFields>
                <customField name="Sort Order" type="NUMBER">
                    <value><![CDATA[10]]></value>
                </customField>
                <customField name="Subsystem" type="SINGLE_CHOICE_SELECT_LIST">
                    <value><![CDATA[Interlocks]]></value>
                </customField>
            </customFields>
            <name><![CDATA[Interlocks - Gate Valve Positions]]></name>
        </testCase>
    </testCases>
</project>

尝试的XSL代码

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//project/testCases">
<xsl:copy>
<xsl:apply-templates select="testCase">
<xsl:sort select="testCase/customFields/customField[1]" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

问题分析与修正后的XSL代码

原XSL排序失败的核心问题有两个:

  1. 排序路径错误:在testCases模板处理testCase节点时,排序的select路径多写了一层testCase/,当前上下文已经是testCase节点,无需重复引用。
  2. 字段定位不可靠:使用索引[1]依赖字段出现顺序,一旦自定义字段顺序变化就会失效,应该通过name属性精准定位“Sort Order”字段。

修正后的XSL代码:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>

<!-- 身份模板:复制所有未被匹配的节点 -->
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<!-- 处理testCases节点,对内部testCase按Sort Order升序排序 -->
<xsl:template match="project/testCases">
  <xsl:copy>
    <xsl:apply-templates select="testCase">
      <xsl:sort select="customFields/customField[@name='Sort Order']/value" data-type="number" order="ascending"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

修正要点

  • 移除排序路径中多余的testCase/,确保上下文匹配正确
  • 通过@name='Sort Order'精准定位目标字段,不依赖字段出现顺序
  • 明确指定order="ascending"(默认升序,添加后逻辑更清晰)

内容的提问来源于stack exchange,提问作者mckee80

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 06:27:12