2011-02-23 58 views
1

我正在写串接一些字符串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') 

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

回答

3

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

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

因为逗号不分离参数的正确的地方结束了这是无效的。然而,这可能为你工作,你depending on the serializer XSLT处理器使用:

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

这周围的参数在双引号,这样你的单引号不冲突。 XSLT处理器应该看到这一点:

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

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

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

哪可以这样使用:

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

更多here

When you apply an XSLT stylesheet to a document, if entities are declared and referenced in that document, your XSLT processor won't even know about them. An XSLT processor leaves the job of parsing the input document (reading it and figuring out what's what) to an XML parser; that's why the installation of some XSLT processors requires you to identify the XML parser you want them to use. (Others include an XML parser as part of their installation.) An important part of an XML parser's job is to resolve all entity references, so that if the input document's DTD declares a cpdate entity as having the value "2001" and the document has the line "copyright &cpdate; all rights reserved", the XML parser will pass along the text node "copyright 2001 all rights reserved" to put on the XSLT source tree.

+0

这不是一个说法? - >'url(''我认为它会转换为“url('” – 2011-02-23 16:09:56

+0

)我的眼球第一次解析错了,请参阅我的更新回答 – 2011-02-23 16:39:03

+0

真的很有用!谢谢。 – 2011-02-23 16:49:55

2

http://www.w3.org/TR/xpath/#NT-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>