简体   繁体   English

xsl:param默认值未被覆盖

[英]xsl:param default value is not being overridden

I have tried reducing my problem to as small a test case as possible. 我尝试将问题减少到尽可能小的测试用例。

I currently have the following stylesheet: 我目前有以下样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template name="set-attrib">
      <xsl:param name="val" select="3"/>
      <xsl:attribute name="value">
         <xsl:value-of select="$val" />
      </xsl:attribute>
   </xsl:template>

   <xsl:template match='@value[parent::element]'>
      <xsl:call-template name="set-attrib">
         <with-param name="val">2</with-param>
      </xsl:call-template>
   </xsl:template>

</xsl:stylesheet>

And applying it to the following XML: 并将其应用于以下XML:

<?xml version="1.0"?>
<element value="0"/>

I get the output: 我得到的输出:

<?xml version="1.0"?>
<element value="3"/>

rather than what I'd expect: 而不是我所期望的:

<?xml version="1.0"?>
<element value="2"/>

It appears that the template set-attrib is ignoring the parameter val that is being set in call-template . 似乎模板set-attrib忽略了在call-template设置的参数val

I have dug around the net for ideas, and tried varied permutations (eg using select="2" in the with-param , or moving the default value of xsl:param into the <!-- Content: template --> , rather than in the select attribute) with no difference. 我在网上挖了一些想法,并尝试了各种排列方式(例如,在with-param使用select="2" ,或者将xsl:param的默认值移到<!-- Content: template --> ,而不是而不是select属性)。

The only way I seem to be able to get it to work is by hard-coding the parameter within the set-attrib template or completely inlining the call-template . 我似乎能够使其正常工作的唯一方法是在set-attrib模板中对参数进行硬编码或完全内联call-template

I cannot see how this is different to other example code; 我看不到这与其他示例代码有何不同; am I missing something blatantly obvious? 我是否缺少明显的东西?

<with-param name="val">2</with-param> needs to be <xsl:with-param name="val">2</with-param> . <with-param name="val">2</with-param>必须为<xsl:with-param name="val">2</with-param> With that change, http://xsltransform.net/bFDb2BS works fine. 进行此更改后, http://xsltransform.net/bFDb2BS可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM