Subject: Re: [xsl] Comments in XPath / XSLT regular expressions? From: Abel Braaksma Online <abel.online@xxxxxxxxx> Date: Wed, 26 Jul 2006 11:44:15 +0200 |
<xsl:variable name="re-extract-filename" > ^.*? <!-- non-greedy: grab everything --> ([^/\\]+) <!-- filename in $1 --> \. <!-- extension-dot --> [^\.]*$ <!-- extension (not-a-dot*) --> </xsl:variable> <xsl:value-of select="replace(., $re-extract-filename, '$1.xml', 'x')" />
Cheers, Abel
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Comments in XPath / XSLT , Michael Kay | Thread | Re: [xsl] Comments in XPath / XSLT , David Carlisle |
RE: [xsl] listing elements & sub-el, Karl | Date | Re: [xsl] Comments in XPath / XSLT , David Carlisle |
Month |