如何使用 xslt 按值对 xml 个元素进行排序
How to sort xml elements by value with xslt
我下面有一个xml。
<parent>
<child1>False</child1>
<child2>True</child2>
<child3>False</child3>
<child4>True</child4>
</parent>
<parent>
<child4>False</child1>
<child5>True</child2>
<child3>False</child3>
<child4>False</child4>
</parent>
按 True 排序后 xml 和最终的 xml 应该看起来像
<parent>
<child4>True</child4>
<child2>True</child2>
<child1>False</child1>
<child3>False</child3>
</parent>
<parent>
<child5>True</child2>
<child4>False</child1>
<child3>False</child3>
<child4>False</child4>
</parent>
用xslt可以吗?我需要 xslt 逻辑来按 True.
对 xml 元素进行排序
xsl:sort
就是您要找的。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="parent">
<parent>
<xsl:for-each select="*">
<xsl:sort select="." order="descending"/>
<xsl:copy-of select='.'/>
</xsl:for-each>
</parent>
</xsl:template>
</xsl:stylesheet>
我下面有一个xml。
<parent>
<child1>False</child1>
<child2>True</child2>
<child3>False</child3>
<child4>True</child4>
</parent>
<parent>
<child4>False</child1>
<child5>True</child2>
<child3>False</child3>
<child4>False</child4>
</parent>
按 True 排序后 xml 和最终的 xml 应该看起来像
<parent>
<child4>True</child4>
<child2>True</child2>
<child1>False</child1>
<child3>False</child3>
</parent>
<parent>
<child5>True</child2>
<child4>False</child1>
<child3>False</child3>
<child4>False</child4>
</parent>
用xslt可以吗?我需要 xslt 逻辑来按 True.
对 xml 元素进行排序xsl:sort
就是您要找的。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="parent">
<parent>
<xsl:for-each select="*">
<xsl:sort select="." order="descending"/>
<xsl:copy-of select='.'/>
</xsl:for-each>
</parent>
</xsl:template>
</xsl:stylesheet>