Checking for missing attribute with cts: query

I have an XML fragment where I want to have different requests based on the presence of an id attribute:

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

      

I 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 identifier exists, then another request is made and a match with the identifier occurs. How to detect missing attribute in MarkLogic?

+3


source to share


1 answer


I entered 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 docs:



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 a document where the ID attribute does not exist.

+2


source







All Articles