XSL用于对XML中的节点进行排序。

huangapple go评论54阅读模式
英文:

XSL for sorting on a node in XML

问题

I have an xml file from an outside source. I'm attempting to bring it into VBA and create a word document out of it. It all works fine except I cannot get the sort to work using XSL (or without it for that matter).

I've googled and chat gpt'ed a bunch of solutions, but none of them are working for me. I'm trying to sort on "Sort Order", which is tagged in the xml as a custom field.

<?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>

The XSL I'm trying to use to sort it. I've tried using Sort Order instead of customField[0], etc

<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>
英文:

I have an xml file from an outside source. I'm attempting to bring it into VBA and create a word document out of it. It all works fine except I cannot get the sort to work using XSL (or without it for that matter).

I've googled and chat gpt'ed a bunch of solutions, but none of them are working for me. I'm trying to sort on "Sort Order", which is tagged in the xml as a custom field.

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

The XSL I'm trying to use to sort it. I've tried using Sort Order instead of customField[0], etc

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

答案1

得分: 2

将以下内容从英文翻译为中文:

将:

<xsl:sort select="testCase/customFields/customField[1]" data-type="number"/>

改为:

<xsl:sort select="customFields/customField[1]" data-type="number"/>

或者更好地改为:

<xsl:sort select="customFields/customField[@name='Sort Order']/value" data-type="number"/>

您目前的代码正在寻找一个作为testCase子元素的testCase元素,但没有找到任何内容。

英文:

Change:

&lt;xsl:sort select=&quot;testCase/customFields/customField[1]&quot; data-type=&quot;number&quot;/&gt;

to:

&lt;xsl:sort select=&quot;customFields/customField[1]&quot; data-type=&quot;number&quot;/&gt;

or even better, to:

&lt;xsl:sort select=&quot;customFields/customField[@name=&#39;Sort Order&#39;]/value&quot; data-type=&quot;number&quot;/&gt;

What you have is looking for a testCase element that is a child of testCase - and not finding anything.

huangapple
  • 本文由 发表于 2023年6月9日 06:21:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定