我发现了一些与此类似的问题, 但努力将解决方案“弯曲”到我需要的地方, 所以很抱歉再次询问。
我有一些像这样的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<ns:Root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns="urn:Test.Namespace"
xsi:schemaLocation="urn:Test.Namespace Test1.xsd"
>
<ns:element1 id="001">
<ns:element2 id="001.1" order="1">
<ns:element3 id="001.1.1" />
</ns:element2>
<ns:element2 id="001.2" order="2">
<ns:element3 id="001.1.2" />
</ns:element2>
</ns:element1>
<ns:element1 id="003">
<ns:element2 id="007.0" order="1">
<ns:element3 id="007.1.1" />
</ns:element2>
</ns:element1>
<ns:element1 id="002">
<ns:element2 id="002.1" order="3">
<ns:element3 id="002.1.1" />
</ns:element2>
<ns:element2 id="002.2" order="4">
<ns:element3 id="002.1.2" />
</ns:element2>
</ns:element1>
</ns:Root>
我已经编写了这个 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="urn:Test.Namespace"
>
<xsl:output indent="no" />
<xsl:template match="text()[not(string-length(normalize-space()))]"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates>
<xsl:sort select="/ns:Root/ns:element1/@id" />
<xsl:copy-of select="." />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="ns:element1">
<xsl:copy-of select="." />
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ns:element2">
<xsl:copy-of select="." />
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ns:element3">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
(我从这里抄写了这个大纲 how to sort xml? )
我想要做的是使用此 XSLT 按 element1
的 id
属性对原始 XML 进行排序并生成 XML。
这个想法是,一旦排序,我就可以使用其他一些 XSLT 对其进行处理以获得最终结果。
不幸的是,这并没有给我任何输出,这让我觉得某个地方有一个非常愚蠢的拼写错误,但我看不到它。
最佳答案
你的问题都在这个匹配的模板里
<xsl:template match="/">
<xsl:apply-templates>
<xsl:sort select="/ns:Root/ns:element1/@id" />
<xsl:copy-of select="." />
</xsl:apply-templates>
</xsl:template>
首先是\
符号匹配文档级元素,它与根 ns:Root 不同,但比它高一层。这意味着当您执行 <xsl:apply-templates>
时所有将选择的都是 ns:root 元素,其中只有一个,因此没有必要对其进行排序!
您可能需要首先像这样匹配根元素,复制它,然后开始对子元素进行排序。
<xsl:template match="/*">
<xsl:copy>
<!-- Code to select and sort childrens -->
</xsl:copy>
</xsl:template>
您遇到的下一个问题是排序语句。您正在使用 xpath 表达式 /ns:Root/ns:element1/@id
,但这是绝对路径,而不是相对路径,因此只会选取文档中第一个 ns:element1 的 @id 属性。
假设您已经定位在根元素上,并且假设它只有 ns:element1 元素作为子元素,您可以这样做
<xsl:apply-templates>
<xsl:sort select="@id" />
</xsl:apply-templates>
您遇到的最后一个问题是您有一个 <xsl:copy-of select="." />
您的 xsl:apply-templates 中的声明是不允许的。您可能应该在此处使用 xsl:copy,如上所示。
还值得指出的是,如果您还没有意识到,最好使用 XSLT identity transform复制现有元素,除非您想以某种方式更改它们。这样您就不必为每种特定类型的元素创建模板。
尝试以下 XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="urn:TestNamespace" >
<xsl:output indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="text()[not(string-length(normalize-space()))]"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates>
<xsl:sort select="@id" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当应用于您的 XML 时,输出以下内容
<ns:Root xmlns:ns="urn:Test.Namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:Test.Namespace Test1.xsd">
<ns:element1 id="001">
<ns:element2 id="001.1" order="1">
<ns:element3 id="001.1.1"/>
</ns:element2>
<ns:element2 id="001.2" order="2">
<ns:element3 id="001.1.2"/>
</ns:element2>
</ns:element1>
<ns:element1 id="002">
<ns:element2 id="002.1" order="3">
<ns:element3 id="002.1.1"/>
</ns:element2>
<ns:element2 id="002.2" order="4">
<ns:element3 id="002.1.2"/>
</ns:element2>
</ns:element1>
<ns:element1 id="003">
<ns:element2 id="007.0" order="1">
<ns:element3 id="007.1.1"/>
</ns:element2>
</ns:element1>
</ns:Root>
关于xml - 使用 XSLT 将 XML 排序为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16302049/