xsl 删除所有非数字字符和前导 1 [英] xsl remove all non-numeric characters and leading 1

查看:46
本文介绍了xsl 删除所有非数字字符和前导 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将传入的电话号码字符串转换为标准格式,该格式不包含任何非数字字符,如果是 1,则去掉前导数字.

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.

例如:

+1 (222) 333-4444 x 5555"变成22233344445555"

"+1 (222) 333-4444 x 5555" becomes "22233344445555"

预先感谢您的帮助!

推荐答案

I.XSLT 1.0 解决方案:

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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>

应用于此 XML 文档时:

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

产生想要的、正确的结果:

22233344445555

说明:

  1. 表达式:translate(.,'0123456789','') 被计算为包含当前节点中所有非数字字符的字符串.

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

我们在表达式中使用上面的 1.:

We use 1. above in the expression:

translate(., translate(.,'0123456789',''), '')

translate(., translate(.,'0123456789',''), '')

这将计算为一个字符串,其中删除了当前节点中的所有非数字字符.

and this evaluates to a string where all non-numeric characters from the current node are deleted.

.3.表达式:(substring($vnumsOnly,1,1)='1') +1)" 如果 $vnumsOnly<的第一个字符,计算结果为 2/code> 是 '1',如果起始字符不是 '1',则计算结果为 1.

.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.我们在以下表达式中使用 3.:

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

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

如果不以1"开头,则计算结果为相同的字符串 $vnumsOnly,如果第一个字符为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'.

二.XPath 2.0 解决方案:

只需使用:

replace(replace(., '[^0-9]', ''), '^1', '')

内部替换删除所有不是 0 到 9(数字)的字符.外部替换删除前导 1(如果存在).

The inner replace removes all characters that aren't 0 through 9 (digits). The outer replace removes the leading 1 (if it exists).

这篇关于xsl 删除所有非数字字符和前导 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆