Re: [xsl] Creating strings with apos and replacing

Subject: Re: [xsl] Creating strings with apos and replacing
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 13 May 2025 20:48:40 -0000
Some general rules:

* In an XML attribute using double quotes as delimiters, write double quotes
as &quot;
* In an XML attribute using single quotes as delimiters, write single quotes
as &apos;

The above replacements give you the string that XPath sees, which should then
follow the rules:

* In a XPath 2.0 string literal using double quotes as delimiters, write
double quotes as two double quotes.
* In a XPath 2.0 string literal using single quotes as delimiters, write
single quotes as two single quotes.

Another option which I like to use in XSLT is:

<xsl:variable name="apos" as="xs:string">'</xsl:variable>
<xsl:variable name="quot" as="xs:string">"</xsl:variable>

and then you can write XPath expressions such as "He said, " || $quot " || "I
don" || $apos || "t" || $quot".

But 4.0 gives you another option: `He said, "I don't"` where the string is
written between backticks.

Michael Kay
Saxonica


> On 13 May 2025, at 21:09, dvint@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>
> I like this one
>
> replace($input, '[^a-z0-9]+', '-', 'i')
>
> more generl and avoids the problem all together ;-)
>
> what does that 'i' and the end do? I thought replace only had 3 parameters.
>
> I wasn't able to get any of the quote options in the replace to work, not
sure what I'm doing wrong there, so this one gets the job done.
>
> ..dan
> On 2025-05-13 12:19, Liam R. E. Quin liam@xxxxxxxxxxxxxxxx
<mailto:liam@xxxxxxxxxxxxxxxx> wrote:
>> On Tue, 2025-05-13 at 18:40 +0000, dvint@xxxxxxxxx wrote:
>>> much working
>>> <xsl:function name="ping:gen_id">
>>> 	<xsl:param name="INSTRING"/>
>>> 	<xsl:value-of select="translate(replace(lower-
>>> case($INSTRING), ' ',
>>> '_'),
>>> 	':()','')"/>
>>> </xsl:function>
>> you could use translate() twice here of course.
>> translate($input, ' ', '_')
>> often i use replace($input, '[^a-z0-9]+', '-', 'i')
>> for generating IDs (and then fix them up to be unique), since e.g.
>> allowing % or < or > or & can also cause interesting problems :)
>>> select="translate($inputString,
>>> &apos;&apos;, '')" />
>>> \xA0\xA0\xA0\xA0 <xsl:value-of select="$outputString" />
>> Your problem here is after the XML parser converts &apos; to ' the
>> XPath processor sees,
>> translate($inputstring, '', '')
>> which is not what you want at all.
>> select="translate($inputString, &quot;'&quot;, '')
>> will turn into
>>    translate($inputString, "'", '')
>> which is what i think you want.

Current Thread