RE: [xsl] Basic template matching issues - I think?

Subject: RE: [xsl] Basic template matching issues - I think?
From: Liam R E Quin <liam@xxxxxx>
Date: Tue, 20 Nov 2012 13:51:04 -0500
On Tue, 2012-11-20 at 17:55 +0000, An OldBloke wrote:

> C:\>java -jar saxon9he.jar -o Result.xml text.xml regextime.xslt
> Error at xsl:analyze-string on line 13 of regextime.xslt:
>   XTDE1140: Error in regular expression: net.sf.saxon.trans.XPathException: Error at
>   character 8 in regular expression "(\d{2})\/(\d{2})\/(\d{4}) (\d{...": invalid
>  escape sequence
> Failed to compile stylesheet. 1 error detected.

Character 8 in the expression would be the \ in \/ and indeed we find if
we check the spec that \/ is not a valid sequence.

Remember that \ introduces things that are special. If you want a / that
is not special, just put a / there.

It's necessary to escape / in ed, sed, vi, if you used / as the
delimiter for the expression --
   s/pattern/replacement with \/ in it/
but that does not apply in XSLT.

The special characters in regular expressions *without* a / are
[ ]  ( )  { }  +  *  ?  ^  $

> 
> 
> I'm looking to re-format the time attribute to a datetime format -
> from '12/11/2012 6:12:04 AM' to '2012-22-12T06:12:04' in this case.
> Unless anyone knows a better way of re-formatting date/times? - I'm
> open to ideas.

Your result doesn't correspond to the input; I'm not sure how you get
2012-22-12 from 12/11/2012. Maybe you mean 22/12/2012.

I'd watch for variants like 2/3/2012 by the way, so use \d{1,2} for the
day and month (or month and day if the date might be in American
format).

So we get to
(\d{{1,2}})/(\d{{1,2}})\/(\d{{4}}) (\d{{1,2}}):(\d{{2}}):(\d{{2}})
which may work.

I'd use a variable to avoid having to double the braces:
<xsl:variable name="datetime-regex"
 select="(\d{1,2})/(\d{1,2})\/(\d{4}) (\d{1,2}):(\d{2}):(\d{2})" />
<xsl:analyze-string select="Time" regex="{$datetime-regex}">
  . . .

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org freenode/#xml

Current Thread