Nicholas Saunders
Nicholas Saunders

Reputation: 764

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

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?

Upvotes: 0

Views: 214

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

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()
}

Upvotes: 2

Related Questions