 |
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

September 2nd, 2007, 04:33 AM
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
captured groups with replace()
In processing text, I'm trying to convert dates of the form "January 1, 1999" or "January 1999", but I'm having trouble using captured groups. For example, I have tried to convert the dates to the form "1999-Jan" using the following:
Code:
<xsl:value-of select="replace(., '(January|February|March|April|May|June|July|August|September|October|November|December)\s+(\d{1,2},\s*)?(\d{4})(\D|$)', concat('$3', '-', substring('$1', 1, 3), '$4'), 'i')"/>
For some reason the substring() invocation doesn't work, and I have found that to be true if I call any other function, including my kti:month-from-name() function.
I'm also confused about why I need quotes around the $3 and $4.
Ian
|

September 2nd, 2007, 06:07 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
The reason for this is that what the replace function is actually performing is this:
<xsl:value-of select="replace(., '(January|February|March|April|May|June|July|Augus t|September|October|November|December)\s+(\d{1,2}, \s*)?(\d{4})(\D|$)', '$3-$1$4'), 'i')"/>
i.e. substring('$1', 1, 3) = '$1'
the substition is done after substring has been performed.
/- Sam Judson : Wrox Technical Editor -/
|

September 2nd, 2007, 11:07 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
As with any function call, the arguments to replace() are simple XPath expressions and they are evaluated independently, without any special treatment given them because they appear in a replace() call. So substring('$1', 1, 3) evaluates to '$1', just as it would anywhere, and substring($1, 1, 3) is a syntax error, just as it would be anywhere.
Try doing this with xsl:analyze-string. That will allow you to use
substring(regex-group(1), 1, 3)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|

September 4th, 2007, 12:43 PM
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Sam and Michael!
I get it now, at least when I think about it, but I'm sure I'm going to make the same mistake again :).
As you suggested Michael, I happily got it working using <analyze-string/>.
Thanks again,
Ian
|
|
 |