XSLT:如何反转树? [英] XSLT: how to reverse the tree?
本文介绍了XSLT:如何反转树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要像这样转换文档:
I need to transform a document like this:
<root>
<products>
<ProductInfo>
<ProductID>0</ProductID>
<ProductName>Hello world!</ProductName>
</ProductInfo>
<M>
<ModelInfo>
<ModelID>0</ModelID>
<ModelName>Hello world!</ModelName>
</ModelInfo>
</M>
</products>
</root>
进入这个:
<root>
<products>
<M>
<ModelInfo>
<ModelName>Hello world!</ModelName>
<ModelID>0</ModelID>
</ModelInfo>
</M>
<ProductInfo>
<ProductName>Hello world!</ProductName>
<ProductID>0</ProductID>
</ProductInfo>
</products>
</root>
所以输出中的所有标签应该是相反的顺序.
So all the tags in the output should be in reversed order.
我需要这个来测试:我需要确保一些外部应用程序以任何顺序接受标签;并且我还需要它来测试我的 XML 架构是否允许以任何顺序标记.
I need this for testing: I need to ensure that some external application accepts the tags in any order; and also I need it to test that my XML Schema allows tags in any order.
推荐答案
不是反转树,而是反转兄弟分支的顺序(在所有级别):
Not reverse the tree, but reverse the order of sibling branches (at all levels):
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()">
<xsl:sort select="position()" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT:如何反转树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文