从xsl:template传递字符串参数并在另一个xsl文件中使用它

时间:2012-08-03 20:14:22

标签: xslt

<xsl:template match="HtmlCode">
    <xsl:copy-of select="child::*|text()"/>
</xsl:template>

<xsl:call-template name="HappyFriend">
    <xsl:with-param name="text" select="'i am a friggin' RRRRROOOOOOOVVVERRRRR~~'"/>
</xsl:call-template> 

<xsl:template name="HappyFriend">
        <xsl:param name="text"/>
        <HtmlCode>
            &lt;span&gt; &lt;%="text"%&gt;   &lt;/span&gt;
        </HtmlCode>
<xsl:template>

不知怎的,我不断得到XSLT问题...我想要做的就是获取变量“text”的值,这是“我是一个frigggin RRROVERRR”出现在ai am frigggggin'RRROOOVVVERRRR ~~ in “HappyFriend”模板。

我做错了什么?

3 个答案:

答案 0 :(得分:9)

几个问题:

- 字符串文字'i am a friggin' RRRRROOOOOOOVVVERRRRR~~'包含不平衡的单引号。你可能想要

<xsl:with-param name="text" select='"i am a friggin&#x27; RRRRROOOOOOOVVVERRRRR~~"'/>

- call-template不能出现在模板定义之外。

- 要参考您应该使用value-of-select的参数,如

 &lt;span&gt; &lt;%="<xsl:value-of select="$text"/>"%&gt;   &lt;/span&gt;

答案 1 :(得分:1)

请参阅FAQ参数

   <xsl:template name="HappyFriend"> 
         <xsl:param name="text"/> 
         <HtmlCode> 
             <span> 
                <xsl:value-of select="$text"/> 
             </span> 
        </HtmlCode>  
     <xsl:template>

答案 2 :(得分:1)

以下是一种正确的方法,可以按照我的意愿进行操作:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="HtmlCode">
        <xsl:copy-of select="child::*|text()"/>
        <xsl:call-template name="HappyFriend">
            <xsl:with-param name="text" select='"i am a friggin&apos; RRRRROOOOOOOVVVERRRRR~~"'/>
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="HappyFriend">
        <xsl:param name="text"/>
        <HtmlCode>
          <span><xsl:value-of select="$text"/></span>
    </HtmlCode>
    </xsl:template>
</xsl:stylesheet>

在以下XML文档上应用此转换时(没有提供!!!):

<HtmlCode/>

产生了想要的正确结果:

<HtmlCode>
   <span>i am a friggin' RRRRROOOOOOOVVVERRRRR~~</span>
</HtmlCode>
相关问题