是否可以在EXSLT str中使用变量:replace?

时间:2013-02-01 17:22:02

标签: xslt

我想用变量中的值替换字符串,如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str" >
<xsl:variable name="my-variable">bar</xsl:variable>
<xsl:template match="text()">
    <xsl:value-of select="str:replace( . ,'foo', $my-variable )"/>
</xsl:template>
</xsl:stylesheet>

当我在文件上运行时,例如,

<?xml version="1.0" encoding="utf-8"?>
<root>foobar</root>

我来自xsltproc / libxslt的错误

XPath error : Invalid type
xmlXPathCompiledEval: evaluation failed
runtime error: file C:/Users/Adam/Documents/WakhiLD/XSL/replace.xsl line 6 element value-of
XPath evaluation returned no result.

在函数调用中使用变量似乎是合理的,所以我不知道我应该做什么。

1 个答案:

答案 0 :(得分:0)

只需使用<xsl:variable name="my-variable" select="'bar'"/>即可。您当前正在创建类型结果树片段的变量,看起来好像扩展函数在传递此类参数时没有对字符串进行任何转换。但是,如上所示简单地创建一个字符串变量就可以解决它。

如果您确实需要将变量创建为结果树片段,请尝试显式转换为字符串,如<xsl:value-of select="str:replace( . ,'foo', string($my-variable) )"/>中所示。