Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
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
Old September 2nd, 2007, 04:33 AM
Authorized User
Join Date: Jul 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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:
<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.


Old September 2nd, 2007, 06:07 AM
samjudson's Avatar
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 -/
Old September 2nd, 2007, 11:07 AM
mhkay's Avatar
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
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Old 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,

Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW CAN SYSTEM DATE N TIME BE CAPTURED IN A TXTBOX cnkumar74 VB Databases Basics 6 February 14th, 2007 02:09 AM
Dynamic Option Groups Otacustes Excel VBA 0 January 13th, 2007 06:30 PM
Users, Groups and Permissions owain SQL Language 17 November 3rd, 2006 07:44 PM
Groups Ma7T XSLT 1 August 25th, 2005 12:01 PM
Groups help in 8.5 for VS.net risu Crystal Reports 0 March 15th, 2004 04:27 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.