How to get only numbers from string with XPath

不打扰是莪最后的温柔 提交于 2019-12-03 13:14:33

Use:

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

This is the so called "double-translate" method, first proposed by @Michael Kay and can be used both in XPath 1.0 and in XPath 2.0.

Of course, in XPath 2.0 using RegeX will generally be more efficient:

replace('abc123def590xyz', '[^\d]', '')

If you can guarantee that the non-digit characters will be only lower-case letters (like in your example), you could do the following in XPath 1:

translate($myString, 'abcdefghijklmnopqrstuvwxyz', '')

You can add other characters to the alphabet string as necessary.

In XPath 2, you could use a regex:

replace($myString, '[^0-9]', '')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!