Concat, quotation marks and apostrophe combination problems

I tried different methods, and also looked around, but could not achieve this. I need to do the following:

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

single line version:

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

The output should be:

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

The problem is "in the text." concat use it to end the line and wait for the next; or end concat. Exclusive or HTML entities do not seem to work.

Any help really appreciated.

Thank!

+4
xslt concat
Jan 09 '12 at 1:40
source share
3 answers

In XML / XSLT, you don't escape backslash characters.

  • In XML, you can use entity references.
  • In XSLT, you can use entity and variable references.

The problem with the apostrophe inside your concat lines is that the XML parser loading XSLT will expand it before concat is evaluated by the XSLT engine; therefore, you cannot use an entity reference for an apostrophe character if it is not enclosed in double quotes (or entity references for double quotes, as demonstrated by the answer of Dimitry Novachev).

  • Use the " object reference for double quotation marks. "
  • Create a variable for the apostrophe character and specify the variable as one of the concat () components

Used in the context of 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;' )" /> 

If you need a 100% XPath solution that avoids the use of XSLT variables, then it is best to respond to a Dimitre request.

If you're worried about how easy it is to read, understand, and support, then Michael Kay’s suggestion to use XSLT variables for quotation and apostrophe might be better.

+7
Jan 09 2018-12-12T00:
source share

No variable required :

Here is an example of how to create the desired output in two ways:

 <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> 

When this conversion is applied to any XML document (not used), the desired output is generated :

 this is "a sample XML_NODE_VALUE"; "using an apostrophe ' in text" ============= this is "a sample XML_NODE_VALUE"; "using an apostrophe ' in text" 
+6
Jan 09 2018-12-12T00:
source share

I find it easiest to declare variables:

 <xsl:variable name="apos">'</xsl:variable> <xsl:variable name="quot">"</xsl:variable> <xsl:value-of select="concat('This is ', $quot, "a sample using ", $apos)"/> 
+4
Jan 09 2018-12-12T00:
source share



All Articles