.net 我可以在 XPath 表达式中使用正则表达式吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/405060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use a Regex in an XPath expression?
提问by ripper234
Something like .//div[@id='foo\d+]to capture div tags with id='foo123'.
类似于.//div[@id='foo\d+]捕获 div 标签的东西id='foo123'。
I'm using .NET, if that matters.
我正在使用 .NET,如果这很重要的话。
回答by Dimitre Novatchev
As other answers have noted, XPath 1.0does not support regular expressions.
正如其他答案所指出的,XPath 1.0不支持正则表达式。
Nonetheless, you have the following options:
尽管如此,您有以下选择:
- Use an XPath 1.0 expression(note the
starts-with()andtranslate()functions) like this:
- 使用 XPath 1.0 表达式(注意
starts-with()和translate()函数),如下所示:
.//div [starts-with(@id, 'foo') and 'foo' = translate(@id, '0123456789', '') and string-length(@id) > 3 ]
Use EXSLT.NET-- there is a way to use its functions directly in XPath expressions without having to use XSLT. The EXSLT extension functions that allow RegEx-es to be used are:
regexp:match(),regexp:replace()andregexp:test()UseXPath 2.0/XSLT 2.0and its inbuilt support for regular expressions(the functions matches(), replace()and tokenize())
使用EXSLT.NET—— 有一种方法可以直接在 XPath 表达式中使用其函数,而无需使用 XSLT。的EXSLT扩展函数允许使用正则表达式-ES是:
regexp:match(),regexp:replace()和regexp:test()使用XPath 2.0/ XSLT 2.0及其对正则表达式的内置支持(函数match()、replace()和tokenize())
回答by Cristian Vat
XPath 2.0 has some functions which support regular expressions: matches(), replace(), tokenize().
XPath 2.0 有一些支持正则表达式的函数:matches(), replace(), tokenize().
In XPath 1.0 there is no regex support.
在 XPath 1.0 中没有正则表达式支持。
For .NET you can use the XPath engine in Saxon.Net to have XPath 2.0 support.
对于 .NET,您可以使用 Saxon.Net 中的 XPath 引擎来获得 XPath 2.0 支持。
So, if using the XPath 2.0 engine in Saxon.NET, your example would turn to: .//div[matches(@id,'foo\d+')].
所以,如果使用Saxon.NET中的XPath 2.0引擎,你的榜样将转向:.//div[matches(@id,'foo\d+')]。
回答by annakata
回答by hoju
I also wanted to do this so created my own basic xpath module.
我也想这样做,所以创建了我自己的基本xpath 模块。

