 |
| 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
|
|
|
|

February 16th, 2012, 07:06 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 17th, 2012, 04:42 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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.
|
|

February 17th, 2012, 08:56 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 17th, 2012, 10:29 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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.
|
|

February 17th, 2012, 10:49 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by samjudson
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?
|
|

February 17th, 2012, 11:47 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Yes, the boolean(number(string)) returns true if the string is a number, and false if it is not.
|
|

February 17th, 2012, 12:17 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
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
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..
|
|

February 17th, 2012, 12:32 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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>
|
|

February 17th, 2012, 12:59 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
I see. Thank you so much, Sam.
Quote:
Originally Posted by samjudson
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>
|
|
|
 |