I need to convert incoming phone number strings to a standardized format that does not have any non-numeric characters and strips off the leading number if it is 1.
For example:
"+1 (222) 333-4444 x 5555" becomes "22233344445555"
Thanks in advance for your help!
I. XSLT 1.0 solution:
This transformation:
when applied on this XML document:
produces the wanted, correct result:
Explanation:
The expression:
translate(.,'0123456789','')
is evaluated to a string that contains all non-numeric characters in the current node.We use 1. above in the expression:
translate(., translate(.,'0123456789',''), '')
and this evaluates to a string where all non-numeric characters from the current node are deleted.
.3. The expression:
(substring($vnumsOnly,1,1)='1') +1)"
evaluates to2
if the first character of$vnumsOnly
is'1'
and it evaluates to1
if the starting character isn't '1'..4. We use 3. in the following expression:
which evaluates to the same string
$vnumsOnly
if it doesn't start with '1' and it evaluates to its substring starting from the 2nd character, if the first character is '1'.II. XPath 2.0 solution:
Just use:
The inner replace removes all characters that aren't 0 through 9 (digits). The outer replace removes the leading 1 (if it exists).
replace(replace(., '[^1-9]', ''), '^1', '') - I am responding to this... Instead of [^1-9]... Use [^0-9] otherwise any 0's in the phone number get deleted. But other than that this fixed my Workday integration issue great!
You could use nested replace xpath functions. The insider would change the first digit to space if it is 1, the outsider would change nondigits to space.
This stylesheet:
With this input:
Output: