在xslt concat函数中转义单引号

kes*_*v84 48 javascript xml xslt

我想在下面的xsl:value-of xsl statment中输出$ ID变量周围的单引号.

<xsl:value-of select="concat('process[@Ref=',$ID,']')"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)

目前它打印

process@Ref=87799989
Run Code Online (Sandbox Code Playgroud)

请让我知道如何实现这一目标.

谢谢,Keshav

Dim*_*hev 42

在XPath 1.0中:

您可以使用内置实体&apos;&quot;

在XSLT 1.0中:

或者,您可以定义您的$Q$APOS变量(将内容(文字"或文字"字符)放在主体中xsl:variable,而不是放在select属性中).

在XPath 2.x中(这也意味着XSLT 2.x和XQuery 1.x)

只需通过输入两个相邻的撇号来转义撇号,通过输入两个相邻的引号来转义引号,如XPath 2.0语言所定义


小智 35

要扩展Dimitre的答案,您可以在XSLT中使用此解决方案:

<xsl:variable name="apos">'</xsl:variable>
<xsl:value-of select="concat('process[@Ref=',$apos,$ID,$apos,']')"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)

  • 变量方法工作并且看起来最具可读性(恕我直言). (3认同)

ken*_*ytm 30

&apos;吗?

<xsl:value-of select="concat('process[@Ref=&apos;',$ID,'&apos;]')"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)

编辑:请参阅Dimitre的答案以获得更好的解决方案.

  • 奇怪的是这有效; 作为' 应该被解码相当于文字'.也许是一个有缺陷的XSLT引擎? (6认同)
  • Yuo need to use both &apos; and the single quote together: ```&apos;'``` (3认同)

小智 5

<xsl:value-of
select="concat('process[@Ref=&apos;',$ID,'&apos;]')"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)

这对我不起作用。我的解决办法是:

<xsl:value-of select="concat(&quot;process[@Ref='&quot;,$oidConstant,&quot;'&quot;)"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)