I would like to do a string replacement with a value from a variable, like this:
<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>
When I run this on a file, e.g.,
<?xml version="1.0" encoding="utf-8"?>
<root>foobar</root>
I this error from 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.
It seems reasonable to use variables in function calls, so I am at a loss as to what I should be doing.
Simply use
<xsl:variable name="my-variable" select="'bar'"/>and it should work. You are currently creating variable of type result tree fragment and it looks as if the extension function is not doing any conversion to a string when passed such an argument. But simply creating a string variable as shown above should solve it.If you really need to create the variable as a result tree fragment then try converting to a string explicitly, as in
<xsl:value-of select="str:replace( . ,'foo', string($my-variable) )"/>.