|
Subject:
|
formatting phone numbers
|
|
Posted By:
|
xslt_student
|
Post Date:
|
8/7/2006 4:10:18 PM
|
I am using (and learning) XSLT 1.0 (which I am limited to for the moment) to solve some "simple" XML -> XML transformations. I have been asked to strip out all non-digits from a phone number field into three fields, <area_code>, <phone>, and <extension>. The input data is entered by humans, so the format varies from document to document. I bought Michael Kay's excellent book "XSLT Programmer's Reference, 2nd ed." some time ago. From this, I am somewhat limited in the string manipulation functions and need to use the translate function. Is there any way to specify more simply what I need to do other than the following?
<!-- output the phone fields --> <xsl:variable name="rawPhone" select="some/path/to/raw/PhoneNumber" /> <xsl:variable name="phoneDigits" select="translate($rawPhone,'0123456789()-./!@#$%*_=+{}[]|;,','0123456789')" /> <area_code><xsl:value-of select="substring($phoneDigits,1,3)"/></area_code> <phone><xsl:value-of select="substring($phoneDigits,4,7)"/></phone> <extension><xsl:value-of select="substring($phoneDigits,11)"/></extension>
NOTE: I also include a space as a character to be removed, but it wraps badly on this forum. I'm not sure of the exact characters that are legal to put in the translate function, but what if the user had entered a '<' or '&'? How would I specify those characters for removal in the translate function? I was hoping I could use regular expressions or other ways to specify sets of characters.
Any help is appreciated,
A humble XSLT student
|
|