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

Subject: Re: [xsl] Quoting meta characters for a regular expression character class
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Mon, 25 Aug 2008 20:20:26 -0700
> Try whether http://www.xsltfunctions.com/xsl/functx_escape-for-regex.html does what you want.
>

Or simply use a more generally applicable function -- the
f:str-dropWhile() function of FXSL like this:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:f="http://fxsl.sf.net/";
exclude-result-prefixes="f"
>
  <xsl:import href="../f/func-str-dropWhile2.xsl"/>
  <xsl:import href="../f/func-standardStringXpathFunctions.xsl"/>
  <xsl:import href="../f/func-str-reverse.xsl"/>

  <xsl:output method="text"/>

  <xsl:template match="/">
      <xsl:variable name="pStr" select=
      "' . ; ; Hello: ;.,...:::'"/>

    '<xsl:sequence select=
     "f:str-reverse(
	       f:str-dropWhile(
		              f:str-reverse($pStr),
		              f:contains('.:,;/')
		              )
	       )"/>'
  </xsl:template>
</xsl:stylesheet>

When the above transformation is applied (to any source xml file --
ignored), the wanted result is produced:

                ' . ; ; Hello: '

Actually, we produce the result in a simple one-liner. In combination
with the function f:str-dropWhile(), the following two FXSL functions
are used in this expression:

    f:str-reverse($someString)   -- reverses a string.

    f:contains($aString, $SecndString)   -- this is the FXSL wrapper
around the standard XPath function contains()

Using this wrapper allows us to refer to the partial application of
f:contains(), when the first argument of the function is known at
runtime.

Finally, here is the code of f:str-dropWhile() I am using (the one
currently in sourceforge is 4 years old, so I just produced a newer
version, which I will upload to the CVS shortly):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:xs="http://www.w3.org/2001/XMLSchema";
 xmlns:f="http://fxsl.sf.net/";
 exclude-result-prefixes="f xs"
>
  <xsl:import href="func-apply.xsl"/>

  <xsl:function name="f:str-dropWhile">
    <xsl:param name="pStr" as="xs:string"/>
    <xsl:param name="pPredicate" as="element()"/>

    <xsl:sequence select=
     "if(not($pStr))
        then ()
        else
          for $vDrop in f:apply($pPredicate,
                                              substring($pStr, 1, 1)
                                              )
                  return
	            if($vDrop)
	              then
	                f:str-dropWhile(substring($pStr, 2),
	                                          $pPredicate
	                                          )
	              else
	                $pStr"
     />
  </xsl:function>
</xsl:stylesheet>


Please, note that f:str-dropWhile() is just like the f:dropWhile()
function, but for the specific case when we have a list of characters
(string).


--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play




On Mon, Aug 25, 2008 at 10:24 AM, Martin Honnen <Martin.Honnen@xxxxxx> wrote:
>
> Houghton,Andrew wrote:
>>
>> 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...
>
> Try whether http://www.xsltfunctions.com/xsl/functx_escape-for-regex.html does what you want.
>
>
> --
>
>        Martin Honnen
>        http://JavaScript.FAQTs.com/

Current Thread