Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concat, quotation mark and apostrophe combination problems

Tags:

concat

xslt

I tried different ways and also looked around but can't get this running. I need to concat the following:

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

one 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 the ' in the text. concat use it to end a string and expect an following ; or the end of concat. Escaping or HTML entities all seems to not work.

Any help is really appreciated.

Thanks!

like image 471
Talisin Avatar asked Dec 03 '22 05:12

Talisin


1 Answers

In XML/XSLT you do not escape characters with a backslash.

  • In XML you can use entity references.
  • In XSLT you can use entity references and variables.

The problem with the apostrophe inside of your concat strings is that the XML parser loading the XSLT will expand it before the concat gets evaluated by the XSLT engine; so you can't use an entity reference for the apostrophe character unless it is wrapped in double quotes (or entity references for double quotes, as Dimitre Novatchev's answer demonstrates).

  • Use the entity reference " for the double quote ".
  • Create a variable for the apostrophe character and reference the variable as one of the components of the concat()

Applied in the context of an 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 Dimitre's answer would be best.

If you are concerned with how easy it is to read, understand, and maintain, then Michael Kay's suggestion to use XSLT variables for the quote and apostrophe might be best.

like image 82
Mads Hansen Avatar answered Jan 22 '23 12:01

Mads Hansen