如何用类似.//div[@id='foo\d+]
的语法匹配具有id='foo123'
属性的
标签。
我正在使用.NET,如果这有影响的话。
正如其他答案所指出的那样,XPath 1.0不支持正则表达式。
尽管如此,您有以下选项:
starts-with()
和translate()
函数),例如:.//div [starts-with(@id, 'foo') and 'foo' = translate(@id, '0123456789', '') and string-length(@id) > 3 ]
使用 EXSLT.NET -- 有一种方法可以直接在XPath表达式中使用其函数而不必使用XSLT。允许使用正则表达式的EXSLT扩展函数包括:regexp:match()
, regexp:replace()
和regexp:test()
。
使用XPath 2.0/XSLT 2.0及其内置的正则表达式支持(函数matches(), replace()和tokenize())
matches()
、replace()
、tokenize()
。.//div[matches(@id,'foo\d+')]
。我也想做这个,所以我创建了自己的基本xpath模块。