2012-10-17 106 views
0

的问题XSL若没有通过参数通过

从通配符templte比赛经过时,我的参数都上来了空。

我的XML数据源:

<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> 

我的XSL:从主模板

初始c:control[@name='table']比赛是一个更广泛的一块XSL架构做,并分裂出通话

<xsl:template match="c:control[@name='table']"> 
    <xsl:call-template name="table" /> 
</xsl:template> 

然后它会在anot中调用一个命名模板她的文件,它不应该改变我的起始引用 - 我应该仍然能够引用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模板。

不知道我要去哪里错...任何帮助将不胜感激。

回答

2

在你对我们的示例代码,调用名为模板℃之后:控制元素匹配

<xsl:template match="c:control[@name='table']"> 
    <xsl:call-template name="table" /> 
</xsl:template> 

这意味着模板内,当前上下文元素是c:控制。但是,在您的示例XML中,c:control的唯一子节点是div元素。因此,当你做你的应用模板....

<xsl:apply-templates select="xhtml:*" mode="table"> 

...它会寻找一个模板匹配XHTML:在这一点上DIV。如果你没有这样的模板,默认模板匹配将会启动,这将忽略该元素并处理其子元素。这不会传递任何参数,所以你的模板匹配xhtml:thead将不会有任何参数值。

一个解决办法是有一个模板,专门匹配的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:*”如果你想要处理更多的元素,使它更通用。

+0

Ahhhh。我认为参数会贯穿到下一个可用的比赛。 给你我的好朋友的荣誉。 非常感谢。 – Kevin