问题描述
假设我已在数据库中插入以下xml.
Let's assume I have inserted the below xml in the DB.
<root>
<name>Dixit</name>
<entry>
<vol>1212</vol>
<title>title1</title>
<isbn>
<value>123456</value>
</isbn>
</entry>
<entry>
<vol>1212</vol>
<title>title1</title>
</entry>
</root>
如何编写cts查询,该查询将返回<entry>
节点,其中<vol>
为 1212 & <title>
作为 title1 &不应具有<isbn>
元素.
How can I write a cts query which will return me the <entry>
nodes with <vol>
as 1212 & <title>
as title1 & should not have <isbn>
element.
对于上述xml,输出应为
For the above xml the output should be.
<entry>
<vol>1212</vol>
<title>title1</title>
</entry>
推荐答案
通常使用cts:not-query(cts:element-query(xs:QName("isbn"), cts:true-query()))
查找缺少isbn
元素的情况,但不幸的是cts:not-query
导致查询查看整个文档,并且您的XML文档有多个条目,带有isbn的条目和没有isbn的条目,都不会产生您希望看到的结果.
One would normally use cts:not-query(cts:element-query(xs:QName("isbn"), cts:true-query()))
to find cases in which the isbn
element is missing, but unfortunately the cts:not-query
causes the query to look at the entire document, and since your XML document has multiple entries, ones with isbn, and ones without, that will not give the result you hope to see.
您将需要使用XPath手动对cts:search
中的结果进行后过滤,例如:
You will either need to post-filter the result from cts:search
manually using XPath, for instance like:
cts:search(
//entry,
cts:and-query((
cts:element-query(xs:QName("vol"), "1212"),
cts:element-query(xs:QName("title"), "title1")
))
)[empty(isbn)]
或拆分您的文档以将每个entry
保存为单独的文档.然后cts:not-query
将起作用,并提供一种更方便扩展的解决方案.
Or split your document to save each entry
as a separate document. Then the cts:not-query
would work, and give a more convenient solution that scales well too.
HTH!
这篇关于搜索在marklogic中没有特定元素的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!