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

RE: [xsl] Comments in XPath / XSLT regular expressions?


Subject: RE: [xsl] Comments in XPath / XSLT regular expressions?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 26 Jul 2006 10:20:38 +0100

> As I see it, XPath 2.0 has that flag too. See XQuery 1.0 and 
> XPath 2.0 Functions and Operators, section 7.6.1.1 Flags:

Yes, but in XPath the "x" flag does not enable comments. This is because the
Perl comment syntax uses newline to mark the end of a comment. In XSLT,
regular expressions will often appear inside XML attributes, where newlines
get normalized to spaces by an XML parser, so we've always adopted the view
that the grammar should never treat newlines differently from spaces.

My own advice is to avoid using regular expressions that are so complex that
they need comments to explain them. If you need to explain them, then it's
also going to be very hard to debug them, and if you hit performance
problems it will be very difficult to analyze the problems. If you can,
break up the task into separate stages, each defined by simpler regular
expressions.

Another approach to commenting, however, is like this:

<xsl:variable name="x" 
              select="replace(., 
     '^.*?([^/\\]+)\.[^\.]*$)', '$1.xml')"/>
<!--     ^non-greedy: grab everything
           ^the last part of the path: does not contain (back)slashes. Grab
it to $1
                    ^the dot separating the extension from the filename 
                      ^not-a-dot until end of string, this is the extension
-->

or if you prefer:

<xsl:variable name="x" 
              select="replace(., '^.*?([^/\\]+)\.[^\.]*$)', '$1.xml')"/>
<!-- 
     .*?         non-greedy: grab everything
     ([^/\\]+)   the last part of the path: does not contain (back)slashes.
Grab it to $1
     \.          the dot separating the extension from the filename 
     [^\.]*      not-a-dot until end of string, this is the extension
-->


(Sorry if the mailer mangles this!)

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


Current Thread
Keywords