在 xpath 和 xml 中使用正则表达式 [英] using regular expressions in xpath and xml

查看:44
本文介绍了在 xpath 和 xml 中使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用正则表达式以及 xml 和 xpath 时遇到了一个小问题.

i have a small problem in using regular expressions along with xml and xpath.

我有一个这样的 xml 文件

I have an xml file like this

messages.xml

messages.xml

<message>
  <text>dog goes woof</text>
</message>
<message>
  <text>cat goes meow, dog goes woof, fish goes blub</text>
</message>

然后我有一个 xpath 表达式,它允许我选择具有文本节点的文本节点,因为狗像这样发出呜呜声

and then i have an xpath expression which allows me to select the text node which has text node as dog goes woof like this

   String expression = "//text[.='dog goes woof']";
   NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(messages, XPathConstants.NODESET);

这里的messages"是指的是 messages.xml 文件的 Document 变量.

here "messages" is the Document variable which refers to the messages.xml file.

当我遍历 nodeList 时,它只选择第一个文本节点.我也想选择另一个包含狗的文本节点.那么我如何在 xpath 表达式中指定来检查包含 dog go woof 的文本节点.

when i iterate through the nodeList, it selects only the first text node. I want to select the other text node too which contains dog goes woof. So how can i specify in the xpath expression to check for text nodes which contains dog goes woof in them.

请告诉我怎么做.

谢谢

推荐答案

你可以使用 contains() 函数或者如果你真的想使用正则表达式你可以使用 matches() 功能

you can use the contains() function or if you really want to use regex you can use the matches() function

"//text[contains(text(), 'dog goes woof')]"

这篇关于在 xpath 和 xml 中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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