[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] analyze-string gotcha/reminder

Subject: Re: [xsl] analyze-string gotcha/reminder
From: John Lumley <John.Lumley@xxxxxxxxxxxxxx>
Date: Wed, 21 Nov 2012 10:07:57 +0000

On 21/11/2012 01:40, Ihe Onwuka wrote:
On Tue, Nov 20, 2012 at 1:35 PM, John Lumley <John.Lumley@xxxxxxxxxxxxxx> wrote:

A useful technique I've found with a multi-case xsl:analyze-string is to use a set of variables for each of the regex alternatives and then join them for a composite alternative expression, viz:

<xsl:variable name="r1">\d+</xsl:variable>
<xsl:variable name="r2">[A-Za-z]+</xsl:variable>
<xsl:analyse-string select="." regex="{string-join(($r1,$r2),'|')}">
       <xsl:when test="matches(.,$r1)">It's a number</xsl:when>
       <xsl:when test="matches(.,$r2)">A word this time....</xsl:when>
This has three advantages:
i) you only define each case once,
ii) dealing with entities such as quotes is much easier and
iii) it's much clearer what the alternatives are.

The only minor niggle is with embedded bracketed substructures '(...)' where
the numbering to access them, regex-group(n), depends upon the position in
the composite sequence.

hmmm but if the Gods had meant us to do this would they have invented flag="x"?
The flag="x" can certainly makes large expressions more readable, but it certainly doesn't help with defining the matches and case tests once only, nor does it help much with awkward characters such as quotes or {} which started this thread.

John Lumley MA PhD CEng FIEE

Current Thread