Loading TOC...


   $element-name as xs:QName*,
   $query as cts:query
) as cts:element-query


Constructs a query that matches elements by name with the content constrained by the query given in the second parameter. Searches for matches in the specified element and all of its descendants. If the query specified in the second parameter includes any element attribute sub-queries, it will search attributes on the specified element and attributes on any descendant elements. See the second example below).

$element-name One or more element QNames to match. When multiple QNames are specified, the query matches if any QName matches.
$query A query for the element to match. If a string is entered, the string is treated as a cts:word-query of the specified string.

Usage Notes

Enabling both the word position and element position indexes ("word position" and "element word position" in the database configuration screen of the Admin Interface) will speed up query performance for many queries that use cts:element-query. The position indexes enable MarkLogic Server to eliminate many false-positive results, which can reduce disk I/O and processing, thereby speeding the performance of many queries. The amount of benefit will vary depending on your data.

You can query for the existence of an element by specifying an empty cts:and-query as the second parameter. For example, the following will match any instance of the specified element:

    cts:and-query( () ))


      "MarkLogic Corporation"))

  => .. relevance-ordered sequence of 'module' elements
  ancestors (or self) of elements with QName 'function'
  and text content containing the phrase 'MarkLogic


let $x := <a attr="something">hello</a>
cts:contains($x, cts:element-query(xs:QName("a"),
         xs:QName("attr"), "something"),
(: returns true :)

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.


The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • This doesn't appear to work with cts:uris
  • It is possible to search just for the occurrence of an element by using a true-query as the second parameter: cts:element-query(xs:QName("myElement"), cts:true-query())
  • what is the diffrence between cts:element-query and cts:element-value-query.As both of the below query retuen same result . cts:search(fn:doc(), cts:element-query( xs:QName("iid"), "7019")) cts:search(fn:doc(), cts:element-value-query( xs:QName("iid"), "7019")) If they both mean the same then what is the use of creating both ..if only one of them can do the work Is there is any special case where they are used for different purpose ??
  • The following cts:element-query example query can be run against the Oscars example database (which you can build with appservices appbuilder). xquery version "1.0-ml"; declare namespace wiki = "http://marklogic.com/wikipedia";  cts:search(//wiki:place,     cts:element-query(       fn:QName("http://marklogic.com/wikipedia","primary"),       "Newport"     ) ) this will just return the wiki:place elements which contain wiki:primary elements wchih contain the text  'Newport'. The "Newport" is just as if you did a cts:word-query e.g. you can replace "Newport" with cts:word-query("Newport") and everything will work just the same. If you find that this query is slow, enable both the word position and element position indexes ("word position" and "element word position" in the database configuration screen of the Admin Interface) which should positively effect query performance for cts:element-query.  
    • Your code is actually cts:element-query(QName(...), cts:word-query("Newport")). When you pass a String to a function that requires a query constraint, it is auto-coerced. So two differences 1) you are using a nested query structure (this word query applies *somewhere* inside this element) which uses two query terms (term lists in the indexes) and intersects them. To be maximally efficient that will need positional indexes in place as well. Second, if the word is deeply nested within the element, the nested query structure will find it, but element-word-query only finds words directly within the element using a special index where possible. Tip: use cts:contains() to see if a query matches some XML or JSON like so cts:contains(<person><name><first>Brenda</first></name></person>, cts:element-word-query(xs:QName("person"), "Brenda")) (: returns false :)