Thursday 24 October 2013

XSLT: Quotation marks and apostrophes

XSLT 1 - Single quotes and apostrophes

An intriguing issue came up when trying to remove an apostrophe from a text string using XSLT 1. After a little research I found this method of getting around it:

It was a simple case of transposing the double quotes and single quotes in the element and function

<xsl:value-of select='translate($textstring, "&apos;", "_")'/>

This worked for both Saxon6 and MS transformation engines

XSLT 1 - Double quotes

For double quotes use:

<xsl:value-of select="translate(regex-group(2),'&quot;', '')"/>

XSLT 2 - Single quotes and apostrophes

When using XSLT 2 the same result can be achieved using:

<xsl:value-of select="translate($textstring, '''', '_')"/>

or:

<xsl:value-of select="replace($textstring, '''', '_')"/>

XSLT 2 - Double quotes

A similar mechanism can be used with XSLT for double quotes, either:

<xsl:value-of select='translate($textstring, """", "_")'/>

or:

<xsl:value-of select='replace($textstring, """", "_")'/>

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Sorry to remove my previous two posts, but the code got deleted from both posts. What I wanted to say is that to delete both ' and " in a single string delimited by single quotation marks, I used '' to match the single quotation mark and \&quote; to match the double quotation mark. I am using the Saxon v9.7.0.11 XSLT v2.0 processor.

    ReplyDelete