英文:
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.
<?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>
答案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:
<xsl:sort select="testCase/customFields/customField[1]" data-type="number"/>
to:
<xsl:sort select="customFields/customField[1]" data-type="number"/>
or even better, to:
<xsl:sort select="customFields/customField[@name='Sort Order']/value" data-type="number"/>
What you have is looking for a testCase
element that is a child of testCase
- and not finding anything.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论