Xslt 将元素排序并移动到新元素中
即使有了这个站点上所有的好提示,我仍然有一些xslt方面的问题。我对它很陌生。我有这个源文件:Xslt 将元素排序并移动到新元素中,xslt,sorting,element,Xslt,Sorting,Element,即使有了这个站点上所有的好提示,我仍然有一些xslt方面的问题。我对它很陌生。我有这个源文件: <?xml version="1.0" encoding="utf-8"?> <file> <id>1</id> <row type="A"> <name>ABC</name> </row> <row type="B"> <name>BCA</n
<?xml version="1.0" encoding="utf-8"?>
<file>
<id>1</id>
<row type="A">
<name>ABC</name>
</row>
<row type="B">
<name>BCA</name>
</row>
<row type="A">
<name>CBA</name>
</row>
</file>
1.
基础知识
BCA
中国篮球协会
我想添加一个元素并按类型对行进行排序,以得到这个结果
<file>
<id>1</id>
<details>
<row type="A">
<name>ABC</name>
</row>
<row type="A">
<name>CBA</name>
</row>
<row type="B">
<name>BCA</name>
</row>
</details>
</file>
1.
基础知识
中国篮球协会
BCA
我可以使用以下命令对行进行排序:
<xsl:template match="file">
<xsl:copy>
<xsl:apply-templates select="@*/row"/>
<xsl:apply-templates>
<xsl:sort select="@type" data-type="text"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
我可以用这个移动行
<xsl:template match="file">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="*[not(name(.)='row')]" />
<details>
<xsl:apply-templates select="row" />
</details>
</xsl:copy>
</xsl:template>
但当我试图将它们结合起来时,我无法给出正确的答案。当我看到事物是如何结合在一起的时候,我希望能对XSLT有更多的了解。因为我正在创建一个新元素
,所以我认为排序必须在创建新的
元素之前完成。我必须使用xslt 1.0。类似的东西似乎可以工作:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="file">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="row[1]/preceding-sibling::*" />
<details>
<xsl:for-each select="row">
<xsl:sort select="@type" data-type="text"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</details>
<xsl:copy-of select="row[last()]/following-sibling::*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
以下是我得到的结果:
<?xml version="1.0" encoding="utf-8"?>
<file>
<id>1</id>
<details>
<row type="A">
<name>ABC</name>
</row>
<row type="A">
<name>CBA</name>
</row>
<row type="B">
<name>BCA</name>
</row>
</details>
</file>
1.
基础知识
中国篮球协会
BCA
谢谢!它工作正常,我理解您的解决方案:)它很容易在其他文件上重用。