XSLT统计<th>数量生成对应radio输入框的问题求助
问题:XSLT中根据数量生成对应的实现方法
编写XSLT代码时,需要统计生成的标签数量,以此确定要创建多少个输入框。尝试创建变量set但遇到作用域问题,求其他实现方法。
输入XML
<MeetingPollingQuestion> <MeetingPollingQuestionId>472</MeetingPollingQuestionId> <MeetingPollingId>66</MeetingPollingId> <MeetingPollingQuestionTypeId>3</MeetingPollingQuestionTypeId> <SequenceOrder>1</SequenceOrder> <IsVisible>true</IsVisible> <MeetingPollingParts> <MeetingPollingParts> <MeetingPollingPartsId>1368</MeetingPollingPartsId> <MeetingPollingPartsTypeId>1</MeetingPollingPartsTypeId> <MeetingPollingQuestionId>472</MeetingPollingQuestionId> <MeetingPollingPartsValues> <MeetingPollingPartsValues> <MeetingPollingPartsValuesId>2270</MeetingPollingPartsValuesId> <MeetingPollingPartsId>0</MeetingPollingPartsId> <MeetingPollingPartsValuesTypeId>1</MeetingPollingPartsValuesTypeId> <QuestionValue>sdd</QuestionValue> <FileManagerId>0</FileManagerId> </MeetingPollingPartsValues> </MeetingPollingPartsValues> </MeetingPollingParts> <MeetingPollingParts> <MeetingPollingPartsId>1370</MeetingPollingPartsId> <MeetingPollingPartsTypeId>3</MeetingPollingPartsTypeId> <MeetingPollingQuestionId>472</MeetingPollingQuestionId> <MeetingPollingPartsValues> <MeetingPollingPartsValues> <MeetingPollingPartsValuesId>2272</MeetingPollingPartsValuesId> <MeetingPollingPartsId>0</MeetingPollingPartsId> <MeetingPollingPartsValuesTypeId>3</MeetingPollingPartsValuesTypeId> <QuestionValue>Yes</QuestionValue> <FileManagerId>0</FileManagerId> </MeetingPollingPartsValues> <MeetingPollingPartsValues> <MeetingPollingPartsValuesId>2273</MeetingPollingPartsValuesId> <MeetingPollingPartsId>0</MeetingPollingPartsId> <MeetingPollingPartsValuesTypeId>3</MeetingPollingPartsValuesTypeId> <QuestionValue>No</QuestionValue> <FileManagerId>0</FileManagerId> </MeetingPollingPartsValues> <MeetingPollingPartsValues> <MeetingPollingPartsValuesId>2274</MeetingPollingPartsValuesId> <MeetingPollingPartsId>0</MeetingPollingPartsId> <MeetingPollingPartsValuesTypeId>3</MeetingPollingPartsValuesTypeId> <QuestionValue>Abstain</QuestionValue> <FileManagerId>0</FileManagerId> </MeetingPollingPartsValues> </MeetingPollingPartsValues> </MeetingPollingParts> <MeetingPollingParts> <MeetingPollingPartsId>1369</MeetingPollingPartsId> <MeetingPollingPartsTypeId>4</MeetingPollingPartsTypeId> <MeetingPollingQuestionId>472</MeetingPollingQuestionId> <MeetingPollingPartsValues> <MeetingPollingPartsValues> <MeetingPollingPartsValuesId>2271</MeetingPollingPartsValuesId> <MeetingPollingPartsId>0</MeetingPollingPartsId> <MeetingPollingPartsValuesTypeId>4</MeetingPollingPartsValuesTypeId> <QuestionValue>Is Required</QuestionValue> <FileManagerId>0</FileManagerId> </MeetingPollingPartsValues> </MeetingPollingPartsValues> </MeetingPollingParts> <MeetingPollingParts> <MeetingPollingPartsId>1371</MeetingPollingPartsId> <MeetingPollingPartsTypeId>5</MeetingPollingPartsTypeId> <MeetingPollingQuestionId>472</MeetingPollingQuestionId> <MeetingPollingPartsValues> <MeetingPollingPartsValues> <MeetingPollingPartsValuesId>2275</MeetingPollingPartsValuesId> <MeetingPollingPartsId>0</MeetingPollingPartsId> <MeetingPollingPartsValuesTypeId>5</MeetingPollingPartsValuesTypeId> <QuestionValue>Row 1</QuestionValue> <FileManagerId>0</FileManagerId> </MeetingPollingPartsValues> <MeetingPollingPartsValues> <MeetingPollingPartsValuesId>2276</MeetingPollingPartsValuesId> <MeetingPollingPartsId>0</MeetingPollingPartsId> <MeetingPollingPartsValuesTypeId>5</MeetingPollingPartsValuesTypeId> <QuestionValue>Row 3</QuestionValue> <FileManagerId>0</FileManagerId> </MeetingPollingPartsValues> </MeetingPollingPartsValues> </MeetingPollingParts> </MeetingPollingParts> </MeetingPollingQuestion>
当前使用的XSLT代码
<xsl:for-each select="MeetingPollingParts/MeetingPollingParts"> <table class="table" style="width:100%;border-color:gray"> <xsl:choose> <xsl:when test="MeetingPollingPartsTypeId = 3"> <thead> <tr> <xsl:for-each select="MeetingPollingPartsValues/MeetingPollingPartsValues"> <xsl:variable name="set" select="MeetingPollingPartsValues/MeetingPollingPartsValues" /> <th> <xsl:value-of select="QuestionValue" disable-output-escaping="yes"/> </th> </xsl:for-each> </tr> </thead> </xsl:when> <xsl:when test="MeetingPollingPartsTypeId = 5"> <tbody> <xsl:for-each select="MeetingPollingPartsValues/MeetingPollingPartsValues"> <tr> <td style="padding: 8px;line-height: 1.42857143;vertical-align: top;border-top: 1px solid #ddd;"> <xsl:value-of select="QuestionValue" disable-output-escaping="yes"/> </td> <td style="padding: 8px;line-height: 1.42857143;vertical-align: top;border-top: 1px solid #ddd;"> <input type="radio"></input> </td> </tr> </xsl:for-each> </tbody> </xsl:when> </xsl:choose> </table> </xsl:for-each>
当前输出
当前会生成两个独立的表格:一个仅包含表头(有Yes、No、Abstain三个<th>标签),另一个仅包含表体(每行只有一个<input type="radio">输入框),无法形成期望的合并表格结构。
期望HTML输出
<table class="table"> <thead> <tr> <th/> <th>Yes</th> <th>No</th> <th>Abstain</th> </tr> </thead> <tbody> <tr> <td class="bold">Row 1</td> <td> <input type="radio"/> </td> <td> <input type="radio"/> </td> <td> <input type="radio"/> </td> </tr> <tr> <td class="bold">Row 3</td> <td> <input type="radio"/> </td> <td> <input type="radio"/> </td> <td> <input type="radio"/> </td> </tr> </tbody> </table>
解决方案
核心思路是不要循环每个MeetingPollingParts生成独立表格,而是在同一个表格内,先获取表头数据的选项集合,再基于这个集合生成表头和对应数量的radio输入框。
修改后的XSLT代码如下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <xsl:template match="MeetingPollingQuestion"> <!-- 定义变量存储表头的选项节点集合 --> <xsl:variable name="header-options" select="MeetingPollingParts/MeetingPollingParts[MeetingPollingPartsTypeId=3]/MeetingPollingPartsValues/MeetingPollingPartsValues"/> <table class="table"> <!-- 生成表头 --> <thead> <tr> <!-- 空的表头单元格 --> <th/> <!-- 循环表头选项生成<th> --> <xsl:for-each select="$header-options"> <th> <xsl:value-of select="QuestionValue"/> </th> </xsl:for-each> </tr> </thead> <!-- 生成表体 --> <tbody> <xsl:for-each select="MeetingPollingParts/MeetingPollingParts[MeetingPollingPartsTypeId=5]/MeetingPollingPartsValues/MeetingPollingPartsValues"> <tr> <!-- 行名称单元格 --> <td class="bold"> <xsl:value-of select="QuestionValue"/> </td> <!-- 基于表头选项数量循环生成radio输入框 --> <xsl:for-each select="$header-options"> <td> <input type="radio"/> </td> </xsl:for-each> </tr> </xsl:for-each> </tbody> </table> </xsl:template> </xsl:stylesheet>
关键说明:
- 变量作用域解决:将
$header-options变量定义在根模板内,作用域覆盖整个表格的表头和表体生成逻辑,避免局部变量无法跨模块访问的问题。 - 统一表格结构:不再为每个
MeetingPollingParts单独创建表格,而是在一个表格内整合表头和表体,符合期望的HTML结构。 - 动态匹配数量:通过循环
$header-options集合,自动生成与表头数量一致的radio输入框,无需手动统计数量。
内容的提问来源于stack exchange,提问作者Jefferson
相关产品推荐
相关产品推荐

