在XSLT模板中调用<xsl:apply-templates>生成下拉框为空的解决方法
解决XSLT中调用模板填充下拉框为空的问题
嘿,我看了你的代码和遇到的问题,下拉框为空主要是两个关键错误导致的,咱们一步步来修复:
问题1:调用模板的方式错误
在处理<USERLIST/record>的模板里,你用了<xsl:apply-templates name="PREFERENCE_LIST/record"/>——但name属性是用来调用命名模板(也就是用<xsl:template name="xxx">定义的模板)的,而你的偏好模板是基于节点匹配的(<xsl:template match="PREFERENCE_LIST/record">)。正确的做法是用select指定完整的节点路径,因为当前上下文是<USERLIST/record>,所以要从根节点/page出发找到偏好列表的记录。
问题2:选中偏好的判断条件错误
原来的偏好模板里,你是和/page/parameters/preference做对比,但实际上你需要匹配当前用户的<SELECTEDPREFERENCE>值。这里最好的方式是在调用模板时把当前用户的选中偏好作为参数传递,这样模板里就能准确判断哪个选项需要标记为selected。
修改后的完整XSLT代码
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /> <xsl:template match="/page"> <div style="margin: 0px; background-color: #ffffff;"> <form action="Useraction" method="post" id="user_search" onsubmit="if (document.getElementById('user_search').action.value=='') return false; else return true;" style="margin: 0px; border: 0px; padding-top: 0px;"> <hr style="color: #003399; width: 100%; height: 3px; border: 0px; margin-top: 0px; margin-bottom: 5px;" /> <table style="width: 100%; text-align: center; border: 0; background-color: #ffffff; margin-bottom: 1px; margin-top: 1px;" cellspacing="0" cellpadding="0"> <tr> <td class="tableheader3" style="white-space: nowrap; width: 30px;height:30px"> <p align="center"> <input class="tabletext" type="button" value="Alle" onclick="javascript:markAll(document.getElementById('user_search'), 'UserId_')" style="width: 50px;" /> </p> </td> <td class="tableheader5" style="width: 30px; text-align: center;">Serial Number</td> <td class="tableheader5" style="white-space: nowrap; width: 60px;">Name</td> <td class="tableheader5" style="white-space: nowrap; width: 40px;">Preference</td> </tr> <tr> <td colspan="12"> <hr style="border-top: 1px solid #1A15B7; background: transparent;"> </hr> </td> </tr> <xsl:if test="USERLIST/record"> <xsl:apply-templates select="USERLIST/record" /> <tr> <td colspan="12"> <img src="images/td_background4.jpg" style="width: 100%; height: 5px; border: 0px;" alt="" /> </td> </tr> </xsl:if> </table> </form> </div> </xsl:template> <xsl:template match="PREFERENCE_LIST/record"> <!-- 接收传递过来的当前用户选中的偏好参数 --> <xsl:param name="selectedPref"/> <option value="{PREFERENCE_ID}"> <!-- 判断当前偏好是否是用户选中的,是的话添加selected属性 --> <xsl:if test="PREFERENCE_ID = $selectedPref"> <xsl:attribute name="selected">selected</xsl:attribute> </xsl:if> <xsl:value-of select="PREFERENCE_ID"/> </option> </xsl:template> <xsl:template match="USERLIST/record"> <tr> <td class="tabletext" style="white-space: nowrap;"> <xsl:value-of select="SERIALNUMER" /> </td> <td class="tabletext" style="white-space: nowrap;"> <xsl:value-of select="NAME" /> </td> <td class="tabletext" style="white-space: nowrap;"> <select class="tabletext" name="preference" style="width:79px;"> <!-- 调用偏好模板时传递当前用户的选中偏好 --> <xsl:apply-templates select="/page/PREFERENCE_LIST/record"> <xsl:with-param name="selectedPref" select="SELECTEDPREFERENCE"/> </xsl:apply-templates> </select> </td> </tr> </xsl:template> </xsl:stylesheet>
关键修改点说明
- 去掉了原来下拉框里多余的
<option>(包括那个绑定SELECTEDPREFERENCE的选项和空选项),因为模板会自动生成所有偏好选项,并且正确标记选中项。 - 在调用偏好模板时,用
select="/page/PREFERENCE_LIST/record"指定正确的节点路径,确保能找到所有偏好记录。 - 通过
<xsl:with-param>把当前用户的SELECTEDPREFERENCE传递给偏好模板,模板里用<xsl:param>接收,然后对比判断是否添加selected属性。
这样修改后,下拉框就能正确显示所有偏好选项,并且自动选中用户对应的偏好值了。
内容的提问来源于stack exchange,提问作者subha Chandran - subhash
相关产品推荐
相关产品推荐

