java 使用 XPath 查询搜索字符串的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5676256/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:14:25  来源:igfitidea点击:

Searching part of a String using XPath query

javaxmlxpath

提问by JavaBits

I am using the following XPath query to search the name of the author of a book and return the book name when it matches the author.

我正在使用以下 XPath 查询来搜索一本书的作者姓名,并在与作者匹配时返回书名。

String rawXPath = String.format("//book[author= '%s']/bookname/text()", authorBook);  

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr 
 = xpath.compile(rawXPath);

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getNodeValue()); 
}

how to modify the search so that I can search in the content ...../content(node name) for a specific word.

如何修改搜索,以便我可以在内容 ...../content(node name) 中搜索特定单词。

Example: String inside the xml content variable: "This book contains the glory and history of our forefathers. And the impact of it in our daily life is immense."

示例:xml 内容变量中的字符串:“这本书包含了我们祖先的荣耀和历史。它在我们日常生活中的影响是巨大的。”

Now I want to search for the word "daily". If it matches daily it will retun me the book name/author name watever the user wants.

现在我想搜索“daily”这个词。如果它每天匹配,它会返回用户想要的书名/作者姓名。

Thanks

谢谢

采纳答案by forty-two

Use the contains()Xpath function.

使用contains()Xpath 函数。

//book[contains(content, '%s')]/bookname

depending a bit of the structure of you input XML

取决于您输入 XML 的一些结构

回答by Dimitre Novatchev

You want:

你想要

//book[contains(content, $searchString)
     and
       author = $wantedAuthor
      ]
       /bookname/text()

This selects the text()nodes that are children of the booknameelement that is a child of any bookelement in the document, the string value of whose contentchild contains the string (contained in) $searchStringand the string value of whose authorelement is the same as the (string contained in) $wantedAuthorvariable

这将选择作为文档中任何元素的子元素的元素的子text()节点的节点,bookname其子book元素的字符串值content包含字符串(包含在),$searchString并且其author元素的字符串值与(包含在)$wantedAuthor变量

In this Xpath expression the variables need to be substituted by specific strings. Also, it assumes that the element contentis a child of book.

在这个 Xpath 表达式中,变量需要被特定的字符串替换。此外,它假定该元素content是 的子元素book

I don't know Java, but suppose that the final Java code wil look like the following:

我不懂 Java,但假设最终的 Java 代码如下所示:

String.format("//book[contains(content, '%s') 
                    and 
                     author= '%s']/bookname/text()", 
              searchString, authorBook);   

回答by JavaBits

Now if u want to search also some term which is inside one node u can do it like this -

现在,如果您还想搜索某个节点内的某个术语,您可以这样做 -

String rawXPath = String.format("//book/title[contains(innerItem, '%s')]/type/text()", value);

Add one backslash for each node and the node name and ur in.

为每个节点添加一个反斜杠以及节点名称和 ur in。

回答by Lucas de Oliveira

if your xml looks like this:

如果您的 xml 如下所示:

 <book>
   <author> Author Name </author>
   <description> This book contains the glory and history of our forefathers. And the impact of it in our daily life is immense.</description>
 </book>

then you can try:

那么你可以尝试:

String rawXPath = "//book//*[contains(text(),\"daily\")]";

which means "give any book element that any child holds a text that contains the word 'daily'.

这意味着“给任何孩子持有包含“每日”一词的文本的任何书籍元素。

I hope that helps you out! cheers!

我希望能帮到你!干杯!