需要找到具有最大表格单元格数的表格行
你好 我正在使用 XSLT 1.0。我的输入看起来像,
<table>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
<td/>
</tr>
</table>
我想知道节点中 td 的最大数量。在本例中,td 的最大数量位于第 3 个 tr 中,因此我的输出应该是 4。需要一个模板来执行此操作。提前致谢
Hi
I am using XSLT 1.0. My input looks like,
<table>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
<td/>
</tr>
</table>
I want to know the maximum number of td in node. In this case, the maximum number of td is in 3rd tr and so my output should be 4. Need a template to do this. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个不使用递归的示例。它仅使用 xsl:for-each 循环遍历 TR 元素,并按每个元素中 TD 元素的数量排序。第一个是最大值。
最大值被放入一个名为 maxCells 的变量中,作为示例,该变量被设为表的属性。
当此样式表应用于上面提供的 HTML 表格时,输出如下:
Here is an example which does not use recursion. It just uses an xsl:for-each to loop through the TR elements, ordering by the number of TD elements in each. The first one is then the maximum.
The maximum is put in a variable, called maxCells which, as an example, is made an attribute of the table.
When this stylesheet is applied to the provided table HTML above, the output is as follows:
我想知道您是否真的想计算单元格或列的数量。您认为下表的正确结果是什么?
如果您只是计算细胞数,那么您期望的是 2 - 这就是前面两个答案所提供的。然而,该表实际上有三列,所以如果这就是您正在寻找的内容(例如转换 XHTML表到 CALS),您需要稍微调整 @Tim C 的解决方案:将 count(td) 替换为 sum(td/@colspan) + count(td[not(@ colspan)]) 都在 元素和 中。
不幸的是,即使这样,计算也无法在所有情况下提供正确的列计数。例如,当给出以下内容时,它会得出 2 而不是 3:
我不知道如何解决这个问题。我也从未在实时数据中看到过它(敲木头)。
还有一件事。我的业力不足以评论@Tim C的答案,但我需要写下这个以免我忘记:样式表是错误的,因为它按词法对单元格计数进行排序(即它认为“120”<“19”<“5”) ") 因此,如果您的一行有 5 个单元格,另一行有 10 个单元格,那么您最多会得到 5 个单元格。通过将 data-type="number" 添加到 标记即可轻松解决此问题。
I'd like to know if you are really trying to count cells or rather columns. What would you consider the correct result for the following table?
If you're just counting cells then you expect 2 - and that's what both previous answers provide. The table has actually three columns, however, so if that's what you're looking for (like when converting XHTML tables to CALS) you need to tweak @Tim C's solution a bit: replace count(td) with sum(td/@colspan) + count(td[not(@colspan)]) both in the <xsl:sort/> element and in the <xsl:value-of/>.
Unfortunately, even then the calculation fails to provide correct column counts in all cases. For example, it comes up with 2 instead of 3 when given this:
I have no idea how to solve that one. I've never seen it in live data either (knock on wood).
One more thing. My karma is not sufficient to comment on @Tim C's answer but I need to write this lest I forget: the stylesheet is wrong in that it sorts the cell counts lexically (i.e. it thinks "120"<"19"<"5") so if you have a row with 5 cells and another one with 10 you will get 5 as maximum. This is easy to fix by adding data-type="number" to the <xsl:sort/> tag.
你需要使用递归。基本上,下面列出的 running-max 模板针对表的每个 tr 子元素运行。它首先应用于第一个子 tr 元素,计算 td 元素的数量,将其与运行的最大值进行比较,然后继续对后续兄弟元素(如果有)执行相同的操作。
You need to use recursion. Basically, the running-max template listed below runs for every tr child element of a table. It is first applied for the first child tr element, calculates the number of td elements, compares it to a running max, and then continues to do the same for following siblings, if there are any.