Skip to content

Instantly share code, notes, and snippets.

@holmesw
Created September 7, 2023 18:49
Show Gist options
  • Save holmesw/7091d5ab14e1d79994c1156f4a2c6201 to your computer and use it in GitHub Desktop.
Save holmesw/7091d5ab14e1d79994c1156f4a2c6201 to your computer and use it in GitHub Desktop.
Find XML Documents Missing an Element Using cts:search
xquery version "3.0";
declare namespace employee =
"http://example.com/employee";
declare namespace xdmp =
"http://marklogic.com/xdmp";
declare namespace cts =
"http://marklogic.com/cts";
(:~
: enable function mapping
: @see http://docs.marklogic.com/guide/xquery/enhanced#id_55459
:)
declare option xdmp:mapping "true";
(:~
: store cts:query for later use
:)
declare variable $cts-query as cts:query :=
cts:and-query((
cts:collection-query("employees"),
cts:not-query(
cts:element-query(
xs:QName("employee:department-id"),
cts:true-query()
)
),
()
));
(:
assume that the following documents are loaded and visible to user
xdmp:document-insert(
"/employee/dbef4d2a1b0166323a9f02410c2a67c2.xml",
<employee xmlns="http://example.com/employee">
<employee-id>dbef4d2a1b0166323a9f02410c2a67c2</employee-id>
<department-id>X</department-id>
<!-- more data to follow -->
</employee>,
xdmp:default-permissions("/employee/dbef4d2a1b0166323a9f02410c2a67c2.xml"),
(xdmp:default-collections("/employee/dbef4d2a1b0166323a9f02410c2a67c2.xml"), "employees")
),
xdmp:document-insert(
"/employee/ba80773a20cc75f17388a423dce7ae56.xml",
<employee xmlns="http://example.com/employee">
<employee-id>ba80773a20cc75f17388a423dce7ae56</employee-id>
<!-- more data to follow -->
</employee>,
xdmp:default-permissions("/employee/ba80773a20cc75f17388a423dce7ae56.xml"),
(xdmp:default-collections("/employee/ba80773a20cc75f17388a423dce7ae56.xml"), "employees")
)
:)
cts:search(
fn:collection("employees"),
$cts-query,
("unfiltered")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment