xslt 按子元素计数排序 [英] xslt sorting by child element count
问题描述
我正在尝试通过查询 xml 文档来创建 html 表.我正在使用 xslt.
问题来了.父"节点包含许多子"节点.我必须o/p一个包含@name的父表和按排序顺序(降序)子"节点数的表.所以我在做
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="parent[count(child) > 3]"><表格边框=1"><xsl:for-each select="."><xsl:sort select="{count(child)}" data-type="number" order="descending"/><tr><td><b><xsl:value-of select="@name"/></b></td><td><xsl:value-of select="count(child)"/></td></tr></xsl:for-each></html></xsl:模板><xsl:template match="text()"/></xsl:stylesheet>
我得到了 html,但唯一的问题是我没有按照子元素的数量排序.我怀疑我错误地使用了计数 xsl:sort?你能帮忙吗?
输入xml
<parent name="abc" attr1="22664136" attr2="647500"><child百分比="11">aaa</child><child百分比="35">bbb</child><child百分比="50">ccc</child></父母><parent name="ggg" attr1="3249136" attr2="28750"/><parent name="ghi" attr1="29183032" attr2="2381740"><child2><name>ppp</name><attr1>1507241</attr1></child2></父母><parent name="qwe" attr1="10342899" attr2="1246700"/><parent name="lkj" attr1="65647" attr2="440"><child百分比="100">jjj</child></父母></外>
提供的 XSLT 代码有很多错误!
最大的问题在这里:
<块引用> <xsl:sort select="{count(child)}" data-type="number" order="descending"/><tr><td><b><xsl:value-of select="@name"/></b></td><td><xsl:value-of select="count(child)"/></td></tr></xsl:for-each>
这不会执行任何有意义的排序,因为要排序的节点的节点集只包含一个节点——当前节点.
下一个问题来了:
在 XSLT 指令的任何 select
属性中不应该有任何 AVT——您需要删除大括号.
第三个问题是排序的指定太晚了——在模板数学运算parent
中. 父母没有任何自己有孩子
孩子.
解决方案:纠正上面讨论的所有主要问题,可能会得到以下代码:
<xsl:template match="/*"><表格边框=1"><xsl:for-each select="parent"><xsl:sort select="count(child)" data-type="number" order="descending"/><tr><td><b><xsl:value-of select="@name"/></b></td><td><xsl:value-of select="count(child)"/></td></tr></xsl:for-each></html></xsl:模板><xsl:template match="text()"/></xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<parent name="abc" attr1="22664136" attr2="647500"><child百分比="11">aaa</child><child百分比="35">bbb</child><child百分比="50">ccc</child></父母><parent name="ggg" attr1="3249136" attr2="28750"/><parent name="ghi" attr1="29183032" attr2="2381740"><child2><name>ppp</name><attr1>1507241</attr1></child2></父母><parent name="qwe" attr1="10342899" attr2="1246700"/><parent name="lkj" attr1="65647" attr2="440"><child百分比="100">jjj</child></父母></外>
生成想要的排序结果:
<表格边框=1"><tr><td><b>abc</b></td><td>3</td></tr><tr><td><b>lkj</b></td><td>1</td></tr><tr><td><b>ggg</b></td><td>0</td></tr><tr><td><b>ghi</b></td><td>0</td></tr><tr><td><b>qwe</b></td><td>0</td></tr></html>
I am trying to create an html table by querying on an xml document. I am using xslt.
Here is the problem. "parent" node contains many "child" nodes. I have to o/p a table that contains @name of parent and count of "child" nodes in sorted order(descending). So I am doing
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="parent[count(child) > 3]">
<html>
<table border="1">
<xsl:for-each select=".">
<xsl:sort select="{count(child)}" data-type="number" order="descending"/>
<tr>
<td><b><xsl:value-of select="@name" /></b></td>
<td><xsl:value-of select="count(child)" /></td>
</tr>
</xsl:for-each>
</table>
</html>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
I get the html however the only problem is I am not getting it in sorted order by count of child elements. I suspect I am using count incorrectly xsl:sort? Can you help?
Input xml
<outer>
<parent name="abc" attr1="22664136" attr2="647500">
<child percentage="11">aaa</child>
<child percentage="35">bbb</child>
<child percentage="50">ccc</child>
</parent>
<parent name="ggg" attr1="3249136" attr2="28750"/>
<parent name="ghi" attr1="29183032" attr2="2381740">
<child2>
<name>ppp</name>
<attr1>1507241</attr1>
</child2>
</parent>
<parent name="qwe" attr1="10342899" attr2="1246700"/>
<parent name="lkj" attr1="65647" attr2="440">
<child percentage="100">jjj</child>
</parent>
</outer>
There are numerous mistakes in the provided XSLT code!
The biggest problem is here:
<xsl:for-each select="."> <xsl:sort select="{count(child)}" data-type="number" order="descending"/> <tr> <td><b><xsl:value-of select="@name" /></b></td> <td><xsl:value-of select="count(child)" /></td> </tr> </xsl:for-each>
This will not perform any meaningful sort, because the node-set of the nodes to be sorted contains only one node -- the current node.
The next problem is here:
<xsl:sort select="{count(child)}" data-type="number" order="descending"/>
There shouldn't be any AVT in any select
attribute of an XSLT instruction -- you need to remove the curly braces.
The 3rd problem is that the sort is specified too-late -- inside the template mathcing parent
. A parent doesn't have any children that themselves have child
children.
Solution: Correcting all major problems, discussed above, one may arrive at the following code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*">
<html>
<table border="1">
<xsl:for-each select="parent">
<xsl:sort select="count(child)" data-type="number" order="descending"/>
<tr>
<td>
<b>
<xsl:value-of select="@name" />
</b>
</td>
<td>
<xsl:value-of select="count(child)" />
</td>
</tr>
</xsl:for-each>
</table>
</html>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<outer>
<parent name="abc" attr1="22664136" attr2="647500">
<child percentage="11">aaa</child>
<child percentage="35">bbb</child>
<child percentage="50">ccc</child>
</parent>
<parent name="ggg" attr1="3249136" attr2="28750"/>
<parent name="ghi" attr1="29183032" attr2="2381740">
<child2>
<name>ppp</name>
<attr1>1507241</attr1>
</child2>
</parent>
<parent name="qwe" attr1="10342899" attr2="1246700"/>
<parent name="lkj" attr1="65647" attr2="440">
<child percentage="100">jjj</child>
</parent>
</outer>
the wanted-sorted result is produced:
<html>
<table border="1">
<tr>
<td><b>abc</b></td>
<td>3</td>
</tr>
<tr>
<td><b>lkj</b></td>
<td>1</td>
</tr>
<tr>
<td><b>ggg</b></td>
<td>0</td>
</tr>
<tr>
<td><b>ghi</b></td>
<td>0</td>
</tr>
<tr>
<td><b>qwe</b></td>
<td>0</td>
</tr>
</table>
</html>
这篇关于xslt 按子元素计数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!