与xslt中的转义字符串联?

时间:2011-02-23 15:57:46

标签: xslt

我正在编写连接字符串的xslt代码:

<xsl:attribute name='src'>
    <xsl:value-of select="concat('url(&apos;', $imgSrc, '&apos;)')" />
</xsl:attribute>

由于某些原因我无法使用它,我不断收到此错误:

Unknown function - Name and number of arguments do not match any function signature in the static context - 'http://www.w3.org/2005/xpath-functions:concat'

while evaluating the expression: select="concat('url(&apos;', $imgSrc, '&apos;)')"

有什么想法吗?

THX

====================

修改

我想要:

url('some_path')

撇号有问题,但现在它不起作用。

2 个答案:

答案 0 :(得分:3)

&apos;引用由解析XSLT的XML解析器解析。您的XSLT处理器永远不会看到它们。您的XSLT处理器看到的是:

concat('url('', $imgSrc, '')') 

这是无效的,因为逗号最终没有在正确的位置分隔参数。但是,这可能对您有用,您的XSLT处理器使用depending on the serializer

concat(&quot;url('&quot;, $imgSrc, &quot;')&quot;)

这用双引号括起参数,这样你的单引号就不会发生冲突。 XSLT处理器应该看到:

concat("url('", $imgSrc, "')")

另一种选择是定义一个变量:

<xsl:variable name="apos" select='"&apos;"'/>

可以这样使用:

 concat('url(', $apos, $imgSrc, $apos, ')')

更多here

  

将XSLT样式表应用于   文档,如果声明实体和   在该文档中引用了您的XSLT   处理器甚至不会知道它们。   XSLT处理器离开了   解析输入文档(阅读它   并弄清楚什么是什么   XML解析器;这就是为什么   安装一些XSLT处理器   要求您识别XML   你希望他们使用的解析器。 (其他   包括XML解析器作为其中的一部分   安装。)一个重要的部分   XML解析器的工作就是解决所有问题   实体引用,如果是   输入文档的DTD声明了一个cpdate   实体具有值“2001”和   该文件的行为“版权所有   &安培; cpdate;保留所有权利“,XML   解析器将沿文本节点传递   “版权所有2001保留所有权利”   放上XSLT源代码树。

答案 1 :(得分:2)

来自http://www.w3.org/TR/xpath/#NT-Literal

[29]    Literal    ::=    '"' [^"]* '"'  |  "'" [^']* "'" 

意味着XPath文字字符串值不能将分隔符也作为内容的一部分。

为此,您应该使用主语言。在XSLT中:

<xsl:variable name="$vPrefix">url('</xsl:variable>
<xsl:variable name="$vSufix">')</xsl:variable>
<xsl:attribute name="src">
     <xsl:value-of select="concat($vPrefix, $imgSrc, $vSufix)" />
</xsl:attribute> 

或者更合适:

<xsl:attribute name="src">
     <xsl:text>url('</xsl:text>
     <xsl:value-of select="$imgSrc"/>
     <xsl:text>')</xsl:text>
</xsl:attribute> 
相关问题