RE: [xsl] XPath 2.0 Regex misunderstanding

Subject: RE: [xsl] XPath 2.0 Regex misunderstanding
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 19 Jan 2007 16:52:38 -0000
> I have a date element:
> 
> example 
> 
> <DATE>11/01/2006</DATE>

By induction from the examples below, I guess this is 1st November? I'll try
to exercise the necessary mental gymnastics.
> 
> I'm trying to write an XPath 2.0 Regex to winnow some of the 
> more obvious date format errors. I have tried for about a 
> half-hour, and I admit to being stumped.
> 
> Here is the relevant part of the template:
> 
> <xsl:when 
> test="matches(DATE,'[0-1][0-2]/[0-3][0-9]/2006')"><bad-date 
> /></xsl:when>
> 
> These dates passed the test:
> 
> <DATE>07/18/2006</DATE>
> <DATE>07/12/2006</DATE>
> <DATE>09/25/2006</DATE>

I assume that "passing" means they didn't match the regex, and therefore
didn't output "<bad-date/>"? More gymnastics...
> 
> While these failed:
> 
> <DATE>10/24/2006</DATE>
> <DATE>10/18/2006</DATE>
> <DATE>10/10/2006</DATE>
> <DATE>10/02/2006</DATE>
> <DATE>01/19/2006</DATE>
> 
> It appears any value with a "1" in either the first or second 
> position is failing.

No, any value with a digit other than [0-2] in the second position is
"passing". Which is what you asked for.

If the first two digits are supposed to be MM, that is, in the range 00 -
12, then the second digit can range from 0-9. If you want a precise test,
write

(0[1-9]|1[0-2])

Michael Kay
http://www.saxonica.com/

Current Thread