concat、引号和撇号组合问题



我尝试了不同的方法,也四处寻找,但无法运行。我需要插入以下内容:

"concat( 
    'this is; "a sample',
    //XML_NODE,
    '"; "using an apostrophe',
    ''',
    'in text"'
)"

单行版本:

"concat( 'this is; "a sample', //XML_NODE, '"; "using an apostrophe', ''', 'in text"' )"

输出应为:

this is "a sample XML_NODE_VALUE"; "using an apostrophe ' in text"

问题是文本中的"。concat使用它来结束一个字符串,并期望有以下内容;或concat的末端。转义或HTML实体似乎都不起作用。

非常感谢您的帮助。

谢谢!

在XML/XXSLT中,不能用反斜杠转义字符。

  • 在XML中,可以使用实体引用
  • 在XSLT中,可以使用实体引用和变量

concat字符串中的撇号问题在于,加载XSLT的XML解析器将在XSLT引擎评估concat之前对其进行扩展;因此,除非撇号字符用双引号括起来,否则不能对其使用实体引用(或者Dimitre Novatchev的答案所示的双引号的实体引用)。

  • 将实体引用"用于双引号"
  • 为撇号字符创建一个变量,并将该变量作为concat()的组件之一引用

应用于XSLT:的上下文

<xsl:variable name="apostrophe">'</xsl:variable>
<xsl:value-of select="concat( 
            'this is; &quot;a sample',
            //XML_NODE,
            '&quot;; &quot;using an apostrophe ',
            $apostrophe,
            ' in text&quot;'
            )" />

如果您需要一个避免使用XSLT变量的100%XPath解决方案,那么Dimitre的答案将是最好的。

如果您关心它的阅读、理解和维护有多容易,那么Michael Kay建议使用XSLT变量作为引号和撇号可能是最好的。

不需要变量

下面是一个如何通过两种方式产生所需输出的示例:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="/">
  <xsl:text>this is "a sample XML_NODE_VALUE"; "using an apostrophe ' in text"</xsl:text>
  =============
  <xsl:value-of select=
   "concat('this is ',
           '&quot;a sample XML_NODE_VALUE&quot;; &quot;',
           &quot;using an apostrophe &apos; in text&quot;,
           '&quot;'
          )
   "/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于任何XML文档(未使用)时,将生成所需的输出

this is "a sample XML_NODE_VALUE"; "using an apostrophe ' in text"
=============
this is "a sample XML_NODE_VALUE"; "using an apostrophe ' in text"

我发现最简单的解决方案是声明变量:

<xsl:variable name="apos">'</xsl:variable>
<xsl:variable name="quot">"</xsl:variable>
<xsl:value-of select="concat('This is ', $quot, "a sample using ", $apos)"/>

最新更新