Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 November 27th, 2007, 09:18 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Substring-before with Regex


Hi,

I am trying to use the substring-before function with a regex.
I want to grab any number at the beginning of the string the preceeds a non-numerical char.

Normally the <title> would be 22-10 or 6.1

I want to grab the number (e.g. 22, 6) before the first non-numerical char.

This is not working...

substring-before(//ACM:title, [^0-9])"

Thanks

 
Old November 27th, 2007, 09:26 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

substring-before doesn't accept regular expressions as far as I'm aware.

If you're using XSLT 2.0 you have 3 XPATH functions, tokenize(), matches() and replace() which accept regular expression patterns, along with the <xsl:analyze-string> XSLT declaration.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 27th, 2007, 09:32 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thanks... This leads me to another question. I am using the substring-before() to group-by in a for-each-group.

How would I uses the <xsl:analyze-string> to group-by?

Thanks,


 
Old November 27th, 2007, 09:46 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

here's an example:

if <Numbers>1,4,6,7,10</Numbers>

then the following would split this and output each number:

<xsl:analyze-string select="Numbers" regex="[0-9]">
  <xsl:matching-substring>
    <Number><xsl:sequence select="."/></Number>
  </xsl:matching-substring>
</xsl:analyze-string>

e.g.

<Number>1</Number><Number>2</Number>...

The inverse, where you split on the seperator:

<xsl:analyze-string select="Numbers" regex="[,]">
  <xsl:non-matching-substring>
    <Number><xsl:sequence select="."/></Number>
  </xsl:non-matching-substring>
</xsl:analyze-string>


/- Sam Judson : Wrox Technical Editor -/
 
Old November 27th, 2007, 10:43 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I am trying to use the substring-before function with a regex.

Where did you find information that suggested you could do this? You can't.

>I want to grab the number (e.g. 22, 6) before the first non-numerical char.

You could use replace($input, '(^[0-9]*).*', '$1')

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 27th, 2007, 10:44 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>How would I uses the <xsl:analyze-string> to group-by?

The group-by expression can be any XPath expression. It can call a user-defined function, and the user-defined function can use <xsl:analyze-string>.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 27th, 2007, 01:16 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Cool...Thank you!






Similar Threads
Thread Thread Starter Forum Replies Last Post
substring in c# sudhirbharti C# 1 February 21st, 2008 01:29 PM
Get something like SUBSTRING in SQL? VerbatimBOT SQL Server 2000 9 May 11th, 2007 05:06 PM
Substring-after collation bonekrusher XSLT 2 May 11th, 2007 09:31 AM
SubString prasanta2expert Access VBA 1 November 17th, 2006 10:04 AM
rtrim or substring? Help bachuss SQL Server 2000 3 March 1st, 2006 01:05 PM





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