search xmls which do not have particular element in marklogic

眉间皱痕 提交于 2019-12-11 07:28:07

问题


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>

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.

For the above xml the output should be.

<entry>
    <vol>1212</vol>
    <title>title1</title>
</entry>

回答1:


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.

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)]

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!




回答2:


try like, but you will get isbn as well,

cts:search(//entry,     
cts:and-query((cts:element-query(xs:QName("vol"), "1212"), 
cts:element-query(xs:QName("title"), "title1"))))

to completely avoid isbn, fire two cts searches as,

<entry> 
{
(cts:search(//vol, cts:element-query(xs:QName("vol"), "1212")),    
cts:search(//title, cts:element-query(xs:QName("title"), "title1")))
}
</entry>


来源:https://stackoverflow.com/questions/43251178/search-xmls-which-do-not-have-particular-element-in-marklogic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!