How can I add a .dtd validation to an .xml file created by a .xq Query?

Viewed 105

So my query was working fine and I now need to verify the resulting .xml with a .dtd validation. My .xq looked like this before:

< root>

...

...

< /root>

Now it looks like this:

< !DOCTYPE root SYSTEM 'validation.dtd'>

< root>

...

...

< /root>

Running the .xq now, however, throws the following error:

XPST0003 XQuery syntax error near #...as xs:integer external; < !D#:

Expected '--' or '[CDATA[' after '< !'

Static error(s) in query

I don't know what this error means, and I'm unable to find how to fix it

Thanks in advance

1 Answers

To serialize an XML document with a document type declaration, use the fn:serialize() function with the doctype-system parameter:

xquery version "3.1";

fn:serialize(<root/>, map { "doctype-system": "validation.dtd" })

This produces the following string:

<!DOCTYPE root SYSTEM "validation.dtd">
<root/>

For more on this technique, see the function documentation for fn:serialize() at https://www.w3.org/TR/xpath-functions-31/#func-serialize and description of the doctype-system and doctype-public parameters in the XSLT and XQuery Serialization 3.1 Specification at https://www.w3.org/TR/xslt-xquery-serialization-31/#XML_DOCTYPE.

For processors that only support XPath 3.0 or that have not yet implemented the map(*) method of specifying serialization parameters, you can use this form:

xquery version "3.0";

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

fn:serialize(
    <root/>, 
    <output:serialization-parameters>
        <output:doctype-system value="validation.dtd"/>
    </output:serialization-parameters>
)
Related