问题
当从通配符模板匹配传递时,我的参数将为空。
来源: :
<c:control name="table" flags="all-txt-align-top all-txt-unbold">
<div xmlns="http://www.w3.org/1999/xhtml">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
</tr>
</tbody>
</c:control>My XSL :
最初的c:control[@name='table']匹配与XSL体系结构的一个更广泛的部分有关,并将调用从主模板中分离出来。
<xsl:template match="c:control[@name='table']">
<xsl:call-template name="table" />
</xsl:template>然后,它在另一个文件中调用一个命名模板,它不应该更改我的起始引用--我仍然应该能够引用c:control@name='table‘,就像我在匹配的模板中一样。
<xsl:template name="table">
<xsl:variable name="all-txt-top">
<xsl:if test="contains(@flags,'all-txt-align-top')">true</xsl:if>
</xsl:variable>
<xsl:variable name="all-txt-unbold" select="contains(@flags,'all-txt-unbold')" />
<div xmlns="http://www.w3.org/1999/xhtml">
<table>
<xsl:apply-templates select="xhtml:*" mode="table">
<xsl:with-param name="all-txt-top" select="$all-txt-top" />
<xsl:with-param name="all-txt-unbold" select="$all-txt-unbold" />
</xsl:apply-templates>
</table>
</div>
</xsl:template>如果我在上面的模板中得到了all-txt-top的值,它就会像预期的那样工作。但是,试图将它传递到下面的模板是失败的--我没有得到任何东西。
<xsl:template match="xhtml:thead|xhtml:tbody" mode="table">
<xsl:param name="all-txt-top" />
<xsl:param name="all-txt-unbold" />
<xsl:element name="{local-name()}">
<xsl:apply-templates select="*" mode="table" />
</xsl:element>
</xsl:template>即使我尝试将一个简单的字符串作为参数传递出去,它也无法将其传递到xhtml:thead模板。
不知道我哪里出了问题..。任何帮助都将不胜感激。
发布于 2012-10-17 11:32:24
在您向我们展示的示例代码中,在匹配了c:control元素之后,调用了命名的表模板。
<xsl:template match="c:control[@name='table']">
<xsl:call-template name="table" />
</xsl:template> 这意味着在表模板中,当前的上下文元素是c:control。但是,在示例XML中,c:control的唯一子元素是div元素。因此,当你做你的申请-模板.
<xsl:apply-templates select="xhtml:*" mode="table">..。此时,它将寻找一个与xhtml:div匹配的模板。如果没有这样的模板,默认的模板匹配就会启动,这只会忽略元素并处理它的子元素。这将不会传递任何参数,因此您的模板匹配xhtml:将没有任何参数值。
一种解决方案是有一个模板来专门匹配xhtml:div元素,并传递属性。
<xsl:template match="xhtml:div" mode="table">
<xsl:param name="all-txt-top"/>
<xsl:param name="all-txt-unbold"/>
<xsl:apply-templates select="xhtml:*" mode="table">
<xsl:with-param name="all-txt-top" select="$all-txt-top"/>
<xsl:with-param name="all-txt-unbold" select="$all-txt-unbold"/>
</xsl:apply-templates>
</xsl:template>实际上,如果您想要处理更多的元素,可以将这里的模板匹配更改为"xhtml:*“,以使其更通用。
https://stackoverflow.com/questions/12930705
复制相似问题