Checking absence of attribute with cts:query

杀马特。学长 韩版系。学妹 提交于 2020-01-24 08:50:06

问题


I have an XML fragment where I want to have different queries based in the existence of the id attribute:

<author order="1" 
        id="99999999" 
        initials="A." 
        given-names="Able" 
        surname="Baker" 
        fullname="Able Baker"/>

I have tried:

let $first-query := if ($first)
                then cts:or-query((
                    cts:element-attribute-word-match(xs:QName("author"), xs:QName("given-names"), $first || "*", ("collation=http://marklogic.com/collation/codepoint")),
                    cts:element-attribute-word-match(xs:QName("author"), xs:QName("initials"), $first || "*", ("collation=http://marklogic.com/collation/codepoint"))
                     ))
                else ()

let $last-query := if ($last)
               then cts:element-attribute-word-match(xs:QName("author"), xs:QName("surname"), $last || "*", ("collation=http://marklogic.com/collation/codepoint"))
               else ()

let $author-no-id-query := 
    cts:and-query((
        cts:not-query(
            cts:element-attribute-value-query(xs:QName("author"), xs:QName("id"), "*")
        ),
        $first-query,
        $last-query
    ))

let $query :=    cts:element-query(xs:QName("author"),
                 cts:or-query(($author-no-id-query, $author-id-query
                    )))

If the id exists, then a different query takes place and a match against the id occurs. How do I detect an absence of an attribute in MarkLogic?


回答1:


I have inserted two test documents into the database:

xdmp:document-insert('/example.xml', <author order="1"
        id="99999999"
        initials="A." 
        given-names="Able" 
        surname="Baker" 
        fullname="Able Baker"/>)

xdmp:document-insert('/example2.xml', <author order="1" 
        initials="A." 
        given-names="Able" 
        surname="Baker" 
        fullname="Able Baker"/>)

And run the following query against these documents:

cts:search(fn:doc(),
cts:element-query(xs:QName('author'), cts:and-query((
  cts:not-query(cts:element-attribute-value-query(xs:QName('author'), xs:QName('id'), '*', ("wildcarded")))
 )
)))

This search only matches the document where the ID attribute does not exist.



来源:https://stackoverflow.com/questions/44376518/checking-absence-of-attribute-with-ctsquery

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