我有以下 xml 片段,它出现在很多地方,但 TYPE 元素出现的顺序是随机的。此外,不保证所有类型都可用,例如某些片段可能缺少 Visio 和/或 Outlook 或任何其他节点:
<Applications>
<Type Name="Word">
<Type Name="Excel">
<Type Name="PowerPoint">
<Type Name="Visio">
<Type Name="Outlook">
</Applications>
我想重新排序 Type 元素,以便如果 Type Excel 存在,它将始终位于列表的顶部,依此类推:
if EXCEL exists,
place TYPE Excel at the top.
if WORD exists,
place TYPE Word next,
.
.
.
我尝试过使用 xsl:copy、几个 xsl:if,然后应用特定模板,还有 xsl:whens。不幸的是,这些都不适合我。我查看了另一篇关于重新排序 xml 节点元素的文章,这似乎不是我想要的(它使用了 xsl:call-templates,我没有)。
我有一些像下面这样开始的东西,我想我需要将上面的操作代码添加到它的底部:
XML 已更新
<xsl:template match="Applications">
<xsl:element name="Applications">
<xsl:element name="Type">
<xsl:attribute name="Name">PowerPoint</xsl:attribute>
</xsl:element>
<xsl:element name="Type">
<xsl:attribute name="Name">Outlook</xsl:attribute>
</xsl:element>
<xsl:apply-templates>
<xsl:sort select="string-length(substring-before(';Excel;PowerPoint;Outlook;Word;Visio',@Name))"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
Wanted:
<Applications>
<Type Name="Excel">
<Type Name="PowerPoint">
<Type Name="Outlook">
<Type Name="Word">
<Type Name="Visio">
</Applications>
但得到:
<Applications>
<Type Name="PowerPoint">
<Type Name="Outlook">
<Type Name="Excel">
<Type Name="Word">
<Type Name="Visio">
</Applications>
感谢帮助让这个东西发挥作用...TIA。