I am using the following XQuery code for selecting all .html documents within a collection of exist-db. The script should create an XML document (serialized as JSON) with document URI and title (which is stored as the first H1 element). However the the element remains empty. Why?
xquery version "3.0";
declare option exist:serialize "method=json media-type=text/javascript";
<result> {
let $data-collection := '/db/output'
for $doc in collection($data-collection)
where contains(base-uri($doc), '.html')
return
<item>
<url>{base-uri($doc)}</url>
<title>{$doc/h1/text()}</title>
</item>
}
</result>
I have simplified your lookup for HTML documents, but I think your h1
element is not the root of the document, instead I have assumed that it might be anywhere in the document so have used the descendant-or-self
axis, e.g. //
and as your HTML may be in a namespace I have used the *:
prefix to indicate any namespace.
xquery version "3.0";
declare option exist:serialize "method=json media-type=text/javascript";
<result> {
let $data-collection := '/db/output'
for $doc in collection($data-collection)[ends-with(base-uri(.), '.html')]
return
<item>
<url>{base-uri($doc)}</url>
<title>{$doc//*:h1/text()}</title>
</item>
}
</result>