Q:

Need XQuery syntax for 'if Exists()' behaviour in search api

I have a requirement that we need to perform marklogic search only within the documents which have <associated-bundles> element in it's active version. 
Below is our document structure
 <document document-status='active'>
 <file> 
     <version id=1 version-status='active'>
         <file-name/>
         <associated-bundles>
            <bundle doc-id="100082"></bundle>
            <bundle doc-id="100083"></bundle>
            <bundle doc-id="100000"></bundle>
         </associated-bundles>
         ...
         <file-level-visits>3</file-level-visits>
     </version>
     <version id=2 version-status='archived'>
         <file-name/>
         <uploaded-by>
         ...
         <file-level-visits>2</file-level-visits>
     </version>
     <version id=3 version-status='archived'>
         <file-name/>
         <uploaded-by>
         ...
         <file-level-visits>4</file-level-visits>
     </version>
 </file>

Considering above document structure, I need to perform search only on the documents which have <associated-bundles> element in their active version. 
Note: There is only 1 active version in the document.   
Below is the query to search only within active file "version-status".  But in addition to this, I want to include 'ifExists' check of <associated-bundles> for the document.  
cts:element-query (xs:QName ("version"),cts:and-query ((cts:element-attribute-value-query(fn:QName("","version"), fn:QName("","version-status"), "active", ("lang=en"), 1)
)));

Please let me know, how to achieve this. 

A:

You can check for the existence of an element using cts:element-query() using the syntax below from the documentation:
cts:element-query(xs:QName("my-element"),cts:and-query( () ))

