为什么我不能在XSL中使用apply-templates获取参数?

dar*_*asd 6 xslt parameters

我发现无法使用with-param来处理apply-templates.举个例子,我已经破解了w3schools中给出的例子.

XSL

<xsl:template match="/">
  <xsl:apply-templates>
    <xsl:with-param name="test" select="'has this parameter been passed?'"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="cd">
  <xsl:param name="test"></xsl:param>
  parameter:
  <xsl:value-of select="$test"></xsl:value-of>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

XML

<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>
Run Code Online (Sandbox Code Playgroud)

(希望)你会看到测试参数没有传递给cd模板.我可以在使用call-template时使用它,但不能使用apply-templates.这是怎么回事?我正在使用XSL 1.0.请忽略我传递硬编码参数的事实 - 这只是一个例子.

Mar*_*ell 5

嗯...有趣...... 在.NET中使用XslTransform和使用失败XslCompiledTransform- 但它看起来应该可以工作......好奇......

更新问题seem是匹配; 尝试

<xsl:template match="/catalog"> <!-- CHANGE HERE --> 
  <xsl:apply-templates>
    <xsl:with-param name="test" select="'has this parameter been passed?'"/>
  </xsl:apply-templates>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这对我来说没有任何其他变化.不同之处在于您匹配根节点.当你执行"应用模板"时,它首先级联到目录(使用param),然后级联到cd(没有param).要获得您想要的东西,您需要从目录开始.你可以通过<xsl:vaue-of select="name()"/>在匹配中添加一个来看到这个,然后将其作为"/"和"/ catalog"来尝试.