views:

21

answers:

3

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!

+1  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="/">
        <xsl:variable name="vPhone"
          select="translate(Phone,translate(Phone,'0123456789',''),'')"/>
        <xsl:value-of 
                select="substring($vPhone, 1 + starts-with($vPhone,'1'))"/>
    </xsl:template>
</xsl:stylesheet>

With this input:

<Phone>+1 (222) 333-4444 x 5555</Phone>

Output:

22233344445555
Alejandro
It could done with XPath only, but repeating $vPhone expression...
Alejandro
@Alejandro: +1 for a perfect solution that was submitted even a few seconds before mine. :)
Dimitre Novatchev
@Dimitre: I'm ussing feedreader for SO now ;)
Alejandro
A: 

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.

Gábor Lipták
+2  A: 

I. XSLT 1.0 solution:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:template match="text()">
  <xsl:variable name="vnumsOnly" select=
  "translate(., translate(.,'0123456789',''), '')
  "/>

  <xsl:value-of select=
  "substring($vnumsOnly, (substring($vnumsOnly,1,1)='1') +1)"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>"+1 (222) 333-4444 x 5555</t>

produces the wanted, correct result:

22233344445555

Explanation:

  1. The expression: translate(.,'0123456789','') is evaluated to a string that contains all non-numeric characters in the current node.

  2. 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 to 2 if the first character of $vnumsOnly is '1' and it evaluates to 1 if the starting character isn't '1'.

.4. We use 3. in the following expression:

substring($vnumsOnly, (substring($vnumsOnly,1,1)='1') +1)

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:

replace(replace(., '[^1-9]', ''), '^1', '')
Dimitre Novatchev
Thanks! This works perfectly. I really appreciate the explanation too.
MattM
@Dimitre: +1 for XPath 2.0
Alejandro