XSLT/XPATH - 查找列数最多的表格行 [英] XSLT/XPATH - Find table row with the most columns
问题描述
我正在将 xml 表转换为不同的表规范,我需要确定给定表中任何行中存在的最大列数.所以如果我的 XML 是:
I'm transforming an xml table to a different table spec, and I need to determine the maximum number of columns that are present in any row in a given table. So if my XML is:
<table>
<row>
</column>
</row>
<row>
</column>
</column>
</column>
</row>
<row>
</column>
</column>
</row>
</table>
我需要在表格模板中将3"作为变量,以便我可以按如下方式重写表格:
I need to get "3" as a variable in my table template so that I can rewrite the table as follows:
<table cols="3">
...
我正在做这个,这很愚蠢(但刚好够用......).
I'm doing this, which is very dumb (but just good enough...).
<xsl:template match="table">
<xsl:variable name="high_col_count">
<xsl:choose>
...
<xsl:when test="tr/td[position()=4]">4</xsl:when>
<xsl:when test="tr/td[position()=3]">3</xsl:when>
<xsl:when test="tr/td[position()=2]">2</xsl:when>
...
</xsl:choose>
</xsl:variable>
...
</xsl:template>
我无法确定更好的方法是在不迭代变量的情况下执行此操作,据我所知,由于变量是不可变的,因此在 xslt 中这是不可能的.有没有更简单的方法可以简单地从列数最多的行中检索计数?
I can't determine a better was to do this without iterating over a variable, which as I understand isn't possible in xslt due to variables being immutable. Is there an easier way to simply retrieve the count from the row with the most columns?
推荐答案
尝试改用这个:
<xsl:template match="table">
<table cols="{max(row/count(column))}">
...
</table>
</xsl:template>
这篇关于XSLT/XPATH - 查找列数最多的表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!