嗨,我正在使用XSLT 1.0.我的输入看起来像,
<table>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
<td/>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我想知道节点中td的最大数量.在这种情况下,td的最大数量是在第3个tr中,所以我的输出应该是4.需要一个模板来执行此操作.提前致谢
这是一个不使用递归的示例.它只使用xsl:for-each循环遍历TR元素,按每个元素中TD元素的数量排序.第一个是最大值.
最大值放在一个名为maxCells的变量中,作为一个例子,它是表的一个属性.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates select="table"/>
</xsl:template>
<xsl:template match="table">
<!-- Variable holding the maximum number of cells in a row -->
<xsl:variable name="maxCells">
<xsl:for-each select="tr">
<xsl:sort select="count(td)" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="count(td)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- Copy the table, adding the maximum cells as an attribute -->
<xsl:copy>
<xsl:attribute name="MaxCells">
<xsl:value-of select="$maxCells"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- Identity Transform -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此样式表应用于上面提供的表HTML时,输出如下:
<table MaxCells="4">
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
<td/>
<td/>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2169 次 |
| 最近记录: |