我有以下XML:
<personDetails>
<person>
<name>Mr Test</name>
<paid>Yes</paid>
<details>
<detail>
<id>12345</id>
<address>15 Sample Street</address>
</detail>
</details>
</person>
</personDetails>
我正在尝试在给定某个名称和ID时返回paid
的值。返回的值是&#34; false
&#34;这是不正确的,甚至不在我的XML中。
我当前的查询/ java方法是否正确?
当前查询:
//personDetails/person[name='Mr Test'] and //personDetails/person/details/detail[id ='S0335171']/paid/text()
当前方法:
public String getPaidByNameAndId(String name, String id) {
final String _Name = name;
final String _id = id;
return engine.new Query<String>(PERSON_COLLECTION) {
@Override
protected String query(Collection collection) throws Exception {
XQueryService service = queryService();
ResourceSet resourceSet = service.query(
format("//personDetails/person[name='%s'] and //personDetails/person/details/detail[id ='%s']" +
"/paid/text()"
, StringEscapeUtils.escapeXml(_Name),
StringEscapeUtils.escapeXml(_id)
));
for (String resource : new IterableStringResources(resourceSet)) {
results.add(resource);
}
return results.get(0);
}
}.execute();
}
作为参考,我使用的xQueryService是:
org.exist.xmldb.XQueryService;
答案 0 :(得分:3)
您的整个XPath / XQuery确实返回了一个布尔值(使用and
运算符组合了两个值)。根据您的解释,正确的XPath / XQuery将是:
//personDetails/person[name='Mr Test' and details/detail[id ='S0335171']]/paid/text()
<强> xpath demo
强>