xquerymarklogiccts-search

Checking absence of attribute with cts:query


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?


Solution

  • 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.