How to prepend an xml declaration and processing instructions to an XQuery result?

218 Views Asked by At

How is the result of an XQuery prepended with an xml declaration, and a line linking it to an xslt?

error:

Stopped at /home/nicholas/git/xml/labs.xq, 8/6:
[XPST0003] Processing instruction has illegal name: xml.

query:

xquery version "3.1";


for $doc in db:open("covid")

return 

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="labs.xsl"?>

$doc

so that the result is ready to be used with an xslt. Perhaps placing the declaration and "linking" line of xml outside of the query?

see also:

How to link up XML file with XSLT file?

1

There are 1 best solutions below

1
On BEST ANSWER

The XML declaration can only be output by a serialization option you can declare with e.g.

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'xml';

there is no XML declaration as a node in the XQuery data model.

A processing instruction is a node and can be constructed and prepended for instance with

for $doc in db:open("covid")
return 
  (<?xml-stylesheet type="text/xsl" href="labs.xsl"?>, $doc)

or perhaps cleaner or more explicitly with

for $doc in db:open("covid")
return 
document {
  <?xml-stylesheet type="text/xsl" href="labs.xsl"?>,
  $doc/node()
}