[xsl] Quoting meta characters for a regular expression character class

Subject: [xsl] Quoting meta characters for a regular expression character class
From: "Houghton,Andrew" <houghtoa@xxxxxxxx>
Date: Mon, 25 Aug 2008 12:15:32 -0400
I currently am using XSL 2.0 and have a template that basically looks like:

  <xsl:template name="chopPunctuation" as="xsd:string">
    <xsl:param name="chopString" as="xsd:string" required=yes" />
    <xsl:param name="punctuation" as="xsd:string" required="no"
select="string('.:,;/ ')" />
    <xsl:variable name="rePattern" as="xsd:string"
select="concat('[',$punctuation,']+$')" />
    <xsl:value-of select="replace($chopString,$rePattern,'')" />
  </xsl:template>

Basically the template accepts a string and a list of characters to remove
from the end of the string.  The problem here is that I know I just cannot
drop $punctuation into the created on-the-fly regular expression $rePattern
since I have to account for any meta characters that $punctuation might have.
I'm sure someone has come across this issue and has a function that will quote
the necessary meta characters for a regular expression character class.  Seems
like this should be part of the standard XPath 2.0 functions to hide the
details of regular expressions.  After all Perl has had quotemeta() for quite
some time...


Thanks, Andy.

Current Thread