使用XPath查询搜索String的一部分

| 我正在使用以下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()); 
}
如何修改搜索,以便可以在内容..... / content(节点名称)中搜索特定单词。 示例:xml内容变量中的字符串:\“这本书包含了我们祖先的荣耀和历史。它在我们日常生活中的影响是巨大的。” 现在,我要搜索单词“ daily”。如果每天匹配,它将使我重新调整用户想要的书名/作者名。 谢谢     
已邀请:
使用
contains()
Xpath函数。
//book[contains(content, \'%s\')]/bookname
取决于您输入XML的结构     
你要:
//book[contains(content, $searchString)
     and
       author = $wantedAuthor
      ]
       /bookname/text()
这将选择作为文档中任何
book
元素的子元素的
bookname
元素的子元素的
text()
节点,其whose7ѭ子元素的字符串值包含
$searchString
字符串(包含在其中)和
author
元素的字符串值相同作为contained10ѭ变量(包含在其中的字符串) 在此Xpath表达式中,变量需要用特定的字符串替换。另外,假定元素“ 7”是“ 6”的子元素。 我不懂Java,但假设最终的Java代码如下所示:
String.format(\"//book[contains(content, \'%s\') 
                    and 
                     author= \'%s\']/bookname/text()\", 
              searchString, authorBook);   
    
如果您的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>
那么您可以尝试:
String rawXPath = \"//book//*[contains(text(),\\\"daily\\\")]\";
意思是“给予任何书本元素任何孩子拥有包含单词'daily \'的文本。 希望对您有所帮助!干杯!     
现在,如果您还想搜索一个节点内的某个术语,则可以执行以下操作:
String rawXPath = String.format(\"//book/title[contains(innerItem, \'%s\')]/type/text()\", value);
为每个节点以及节点名称和ur输入一个反斜杠。     

要回复问题请先登录注册