简体   繁体   English

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

[英]Is it possible to use variables in EXSLT str:replace?

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, eg, 当我在文件上运行此文件时,例如,

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

I this error from xsltproc / libxslt 我来自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. 只需使用<xsl:variable name="my-variable" select="'bar'"/> ,它就可以工作。 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) )"/> . 如果确实需要将变量创建为结果树片段,则尝试显式转换为字符串,如<xsl:value-of select="str:replace( . ,'foo', string($my-variable) )"/>

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

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