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 13th, 2005, 10:11 AM
Registered User
 
Join Date: Apr 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can XSLT tell if there is a number as a value.

Hi,
I need to be able to tell if there is number within tags, any ideas
if this can be done?

I currently have something like..

Code:
<xsl:when test="text()[starts-with(.,'10.')]">
    <h3><xsl:value-of select="."/></h3>
</xsl:when>
It currently checks to see if the paragraph starts with the number 10, but can
I check to see if a paragraph starts with any number?



 
Old September 13th, 2005, 11:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can test whether the first character is a digit using

translate(substring(.,1,1), '0123456789', '9999999999') = '9'

I'm not sure if that's exactly the test that you wanted.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 14th, 2005, 03:17 AM
Registered User
 
Join Date: Apr 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply!
It looks very promising, though I'm quite new to XSLT- could you
break down the code for me, as I'm not exactly sure what it does.

thanks for help.

 
Old September 14th, 2005, 03:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There are three operations here.

substring(X,1,1) extracts the first character of the string X

translate(X, 'abcd', 'pqrs') converts every a in X to a p, every b to a q, every c to an r, and every d to an s. In my example, every digit was turned into a '9'.

X = Y, when X and Y are both strings, tests whether they contain the same characters.

So the code is testing whether the first character is a digit.

I gave you an XSLT 1.0 solution: in XSLT 2.0 you can do regular expression tests, for example test="matches(., '\d.*')".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 14th, 2005, 03:35 AM
Registered User
 
Join Date: Apr 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thats great, thanks for the speedy reply!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
XSLT - xml difference with line number mrame XSLT 1 June 4th, 2008 05:49 PM
General XSLT Questions in the XSLT Forum jminatel BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 March 31st, 2008 07:50 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM





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