concat,引号和撇号组合问题

Tal*_*sin 4 xslt concat

我尝试了不同的方式,也环顾四周,但无法运行.我需要连接以下内容:

"concat( 
    'this is; \"a sample',
    //XML_NODE,
    '\"; \"using an apostrophe',
    ''',
    'in text\"'
)"
Run Code Online (Sandbox Code Playgroud)

一行版本:

"concat( 'this is; \"a sample', //XML_NODE, '\"; \"using an apostrophe', ''', 'in text\"' )"
Run Code Online (Sandbox Code Playgroud)

输出应该是:

this is "a sample XML_NODE_VALUE"; "using an apostrophe ' in text"
Run Code Online (Sandbox Code Playgroud)

问题是'在文中.concat使用它来结束字符串并期望跟随; 或者结束.转义或HTML实体似乎都无法正常工作.

任何帮助都非常感谢.

谢谢!

Mad*_*sen 7

在XML/XSLT中,您不会使用反斜杠转义字符.

  • 在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;'
            )" />
Run Code Online (Sandbox Code Playgroud)

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

如果您担心阅读,理解和维护是多么容易,那么Michael Kay建议将XSLT变量用于引用和撇号可能是最好的.


Dim*_*hev 6

无需变量:

以下是如何以两种方式生成所需输出的示例:

<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>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于任何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"
Run Code Online (Sandbox Code Playgroud)