Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're getting burned by two gotchas at once:</p> <ul> <li>awareness of namespaces</li> <li>awareness of document nodes</li> </ul> <p>First, the namespace. The XML output of the http-get function is in a namespace as seen by the top-level element:</p> <pre><code>&lt;response xmlns="xdmp:http-get"&gt; </code></pre> <p>To successfully access elements in that namespace, you need to declare a prefix in your query bound to the correct namespace, and then use that prefix in your XPath expressions. For example:</p> <pre><code>declare namespace h="xdmp:http-get"; //h:code </code></pre> <p>Now lets talk about document nodes. :-)</p> <p>You're trying to access <code>$result</code> as if it is a document node containing an element, but in actuality, it is a sequence of two root nodes (so they're not siblings either). The first one (the one you're interested in here) is a parentless <code>&lt;response&gt;</code> element—<em>not</em> a document containing a <code>&lt;response&gt;</code> element.</p> <p>This is a common gotcha: knowing when a document node is present or not. Document nodes are always invisible when serialized (hence the gotcha), and they're always present on documents stored in the database. However, when you just use a bare element constructor in XQuery (as the http-get implementation does), you construct not a document node but an element node without a document node parent.</p> <p>For example, the following query will return the empty sequence, because it's trying to get the <code>&lt;foo&gt;</code> child of <code>&lt;foo&gt;</code>:</p> <pre><code>declare variable $foo := &lt;foo&gt;bar&lt;/foo&gt;; $foo/foo </code></pre> <p>On the other hand, the following <em>does</em> return <code>&lt;foo&gt;</code>, because it's getting the <code>&lt;foo&gt;</code> child of the document node (which has to be explicitly constructed, in XQuery):</p> <pre><code>$declare variable $doc := document{ &lt;foo&gt;bar&lt;/foo&gt; }; $doc/foo </code></pre> <p>So you have to know how a given function's API is designed (whether it returns a document containing an element or just an element).</p> <p>To solve your problem, don't try to access <code>$result/h:response/h:code</code> (which is trying to get the <code>&lt;response&gt;</code> child of <code>&lt;response&gt;</code>). Instead, access <code>$result/h:code</code> (or more precisely <code>$result[1]/h:code</code>, since <code>&lt;response&gt;</code> is the first of a sequence of two nodes returned by the http-get function).</p> <p>For more information on document nodes, check out this blog article series: <a href="http://community.marklogic.com/blog/document-formats-part1" rel="noreferrer">http://community.marklogic.com/blog/document-formats-part1</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload