Re: [xsl] What is a bulletproof way to assign a variable this value: single quote, value of an element, single quote?

Subject: Re: [xsl] What is a bulletproof way to assign a variable this value: single quote, value of an element, single quote?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 13 May 2024 13:13:14 -0000
On 13/05/2024 15:05, Roger L Costello costello@xxxxxxxxx wrote:
Hi Folks,

The value of $legacy-elmt is this element:

<test>Hello, world</test>

Create a variable which holds between single quotes the value of the element
that $legacy-elmt holds. Do this using a two-step process:

<xsl:variable name="tmp-legacy-elmt-value" as="xs:string+"> <xsl:text>'</xsl:text> <xsl:value-of select="string($legacy-elmt)"/> <xsl:text>'</xsl:text> </xsl:variable>

<xsl:variable name="legacy-elmt-name" as="xs:string">
             <xsl:value-of select="$tmp-legacy-elmt-value" separator=""/>
  </xsl:variable>

$legacy-elmt-value = 'Hello, world'

That appears to work flawlessly, right?

Not so fast.

Suppose the value of $legacy-elmt is this element:

<test>O'Neil</test>

$legacy-elmt-value = 'O'Neil'

That is an error (unmatched single quote).

What is the best way to fix this error?


Which escape mechanism do you want to use, XPath 2 and later one to double the single quote?

B <xsl:variable name="legacy-elmt-value" as="xs:string" select="'''' ||
replace($legacy-elmt, '''', '$0$0') || ''''"/>

Current Thread