使用cts:query检查缺少属性 [英] Checking absence of attribute with cts:query

查看:96
本文介绍了使用cts:query检查缺少属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML片段,我想根据id属性的存在进行不同的查询:

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"/>

我尝试过:

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

如果该ID存在,则将进行另一个查询,并与该ID进行匹配.如何检测MarkLogic中缺少属性?

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?

推荐答案

我已经在数据库中插入了两个测试文档:

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

此搜索仅与ID属性不存在的文档匹配.

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

这篇关于使用cts:query检查缺少属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆