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 February 16th, 2012, 07:06 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Question Can XSLT check specific type?

The input xml like:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<request>
  <address streetName="6th" streetType="Ave"/>
</request>
For the streetName, if it is number with -th, we need the ouput only has the number, but sometimes the streetName="Wath", if so the output should be the same as "Wath".

For this case, the output xml should be:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <adr number="6" type="Ave"/>
</response>
The XSLT version is 1.0.

Thank you in advance.
 
Old February 17th, 2012, 04:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

XSLT 2.0 has various regular expression functions, such as matches().

Code:
<xsl:choose>
  <xsl:when test="matches(@streetName, '\d+(th|nd|rd)')">
  ...
  </xsl:when>
  <xsl:otherwise>
  ..
  </xsl:otherwise>
</xsl:choose>
In XSLT 1.0 you'd have to use substring and other string manipulation functions to do the work more manually.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 17th, 2012, 08:56 AM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Thank you, Sam. Would you give the 1.0 code? I do not know how to check the streetName starts with number and end with "th|rd|nd", 1.0 does not support ends-with function. Thank you.
 
Old February 17th, 2012, 10:29 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You'd have to use substring() and string-length() to get the last two characters. Then if that equal "th", "rd" or "nd" then do the same to get the beginning of the string, and convert that to a number using the number() function.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 17th, 2012, 10:49 AM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
You'd have to use substring() and string-length() to get the last two characters. Then if that equal "th", "rd" or "nd" then do the same to get the beginning of the string, and convert that to a number using the number() function.
Thank you, Sam. Is there any way to check the part before last two characters is a number?
 
Old February 17th, 2012, 11:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yes, the boolean(number(string)) returns true if the string is a number, and false if it is not.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 17th, 2012, 12:17 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Yes, I tried and it works. Just one more issue is that: when
Code:
<xsl:when test="substring-after(address/@streetName,'(th|st|nd|rd|TH|ST|ND|RD)')='' and substring-before(address/@streetName,'(th|st|nd|rd|TH|ST|ND|RD)')!=''">
  <xsl:value-of select="substring-before(address/@streetName,'(th|st|nd|rd|TH|ST|ND|RD)')"/>
</xsl:when>
the (th|st|nd...) would not work, this is to check after "th" whether there is more character and before "th" whether there is character(there is one street called "th street"). why?

Thank you!

Quote:
Originally Posted by samjudson View Post
Yes, the boolean(number(string)) returns true if the string is a number, and false if it is not.

Last edited by JohnKiller; February 17th, 2012 at 12:23 PM..
 
Old February 17th, 2012, 12:32 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

XSLT 1.0 doesn't support regular expressions, so your code is trying to find the exact string "(th|st..." which doesn't exist.

You'd need to create two variables:

Code:
<xsl:variable name="suffix" select="substring(address/@streetName, string-length(address/@streetName)-2,2)"/>
<xsl:variable name="rest" select="substring(address/@streetName, 1, string-length(address/@streetName)-2)"/>
Then test them:

Code:
<xsl:when test="($suffix='th' or $suffix = 'rd' or $suffix='st' or $suffix='TH' or $suffix = 'RD' or $suffix='ST') and boolean(number($rest))">
...
</xsl:when>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 17th, 2012, 12:59 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

I see. Thank you so much, Sam.

Quote:
Originally Posted by samjudson View Post
XSLT 1.0 doesn't support regular expressions, so your code is trying to find the exact string "(th|st..." which doesn't exist.

You'd need to create two variables:

Code:
<xsl:variable name="suffix" select="substring(address/@streetName, string-length(address/@streetName)-2,2)"/>
<xsl:variable name="rest" select="substring(address/@streetName, 1, string-length(address/@streetName)-2)"/>
Then test them:

Code:
<xsl:when test="($suffix='th' or $suffix = 'rd' or $suffix='st' or $suffix='TH' or $suffix = 'RD' or $suffix='ST') and boolean(number($rest))">
...
</xsl:when>





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Check if directory exist ArnArn XSLT 3 November 23rd, 2008 03:44 PM
Getting fields, specific order from xml using xslt Jaipal XSLT 4 July 23rd, 2007 11:35 AM
Display orders for a specific customer - XSLT two_days_late XSLT 3 April 2nd, 2007 04:40 AM
Have any function to check the integer type only calvinpost ASP.NET 1.x and 2.0 Application Design 2 April 1st, 2004 01:05 AM





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