问题描述
我有以下xml:
sample1.xml <root> <subjectInfo> <subject id="001"/> <subject id="002" role="cross"/> </subjectInfo> </root>
sample1.xml <root> <subjectInfo> <subject id="001"/> <subject id="002" role="cross"/> </subjectInfo> </root>
sample2.xml <root> <subjectInfo> <subject id="002"/> <subject id="001" role="cross"/> </subjectInfo> </root>
sample2.xml <root> <subjectInfo> <subject id="002"/> <subject id="001" role="cross"/> </subjectInfo> </root>
我正在搜索subject
的id
属性值为"001",但同一subject
元素的role
(如果存在)的值不是"cross"的文档.示例结果应包含sample1.xml
而不是sample2.xml
I am searching for the documents where value of id
attribute of subject
is "001" but role
(if it's there) of the same subject
element is not "cross".So, In my example the result should contain sample1.xml
and not sample2.xml
我认为可以通过以下查询来完成这项工作:
I thought the following query would do the job:
<code>
cts:search(/root,
cts:near-query((
cts:element-attribute-value-query(xs:QName("subject"),xs:QName("id"),"001"),
cts:not-query(cts:element-attribute-value-query(xs:QName("subject"),xs:QName("role"),"cross"))),0)
)
</code>
但不是(返回一个空序列).请给我一个.
But it does not(returns an empty sequence). Please give me one that does.
推荐答案
正如@wst所说,cts:not-query
匹配两个文档. cts:*
查询匹配文档片段,而不是子树.通过将cts:element-attribute-value-query
构造函数嵌套在cts:element-query
内,可以匹配条件的相反部分.这将匹配sample2.xml
:
As @wst said, the cts:not-query
matches both documents. cts:*
queries match document fragments, not subtrees. You can match the opposite of your conditions, by nesting the cts:element-attribute-value-query
constructors inside of a cts:element-query
. This will match sample2.xml
:
cts:search(/root,
cts:element-query(xs:QName("subject"),
cts:and-query((
cts:element-attribute-value-query(xs:QName("subject"),xs:QName("id"),"001"),
cts:element-attribute-value-query(xs:QName("subject"),xs:QName("role"),"cross")))))
也许您可以调整查询要求,这样就足够了.如果不是,则可以使用except
运算符排除与此搜索匹配的文档.这将匹配sample1.xml
:
Perhaps you can adjust your query requirements so this is sufficient. If not, you can exclude the documents that match this search, using the except
operator. This will match sample1.xml
:
cts:search(/root,
cts:element-attribute-value-query(xs:QName("subject"),xs:QName("id"),"001"))
except
cts:search(/root,
cts:element-query(xs:QName("subject"),
cts:and-query((
cts:element-attribute-value-query(xs:QName("subject"),xs:QName("id"),"001"),
cts:element-attribute-value-query(xs:QName("subject"),xs:QName("role"),"cross")))))
如果文档具有唯一标识符,则可以添加范围索引并使用cts:*-values
函数之一来获取与第二个cts:search
匹配的文档的唯一ID,然后使用cts:not-query
和cts:*-range-query
排除第一个cts:search
中的文档.
If your documents have unique identifiers, you could add range indices and use one of the cts:*-values
functions to get the unique IDs for documents matching the second cts:search
, and then use cts:not-query
and cts:*-range-query
to exclude the documents from the first cts:search
.
这篇关于基于相同元素的多个属性值的Marklogic查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!