[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: analyze-string gotcha/reminder

Subject: Re: analyze-string gotcha/reminder
From: John Lumley <John.Lumley@xxxxxxxxxxxxxx>
Date: Wed, 21 Nov 2012 10:07:57 +0000
Re:  analyze-string gotcha/reminder
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:matching-substring>
     <xsl:choose>
       <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
john@xxxxxxxxxxxxxx

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.