1

When specifying both a DTD and XSD for an XML document, validators seem to validate against the XSD and ignore the DTD completely.

I've verified this in XMLSpy 2018sp1 and Xerces' StdInParse tool.

The standard use case for this is that there is an industry standard DTD we reference (and never edit) which me must validate against. Further to this we further constrain the XML ourselves using our own XSD.

My gut feeling is if both are specified, both the DTD and XSD should have to pass validation for the document to be validated? But perhaps XSD is considered to supersede any DTD?

Example XML - in the below - foo.dtd will never be used to validate the document. It passes the document as valid even if it violates foo.dtd, providing it validates against foo.xsd.

If you remove the attributes from <Foo> and validate again against foo.dtd, XMLSpy for example will suddenly pick-up any DTD issues.... put the XSD location back into Foo, and the errors disappear again!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Foo
  SYSTEM "/usr/local/share/xml/schema/foo/foo.dtd">
<Foo xmlns="https://www.foo.com/schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="https://www.foo.com/schema /usr/local/share/xml/schema/foo/foo.xsd">

Can anyone confirm what is the correct behavior, if any?

Phil
  • 592
  • 6
  • 15
  • 2
    There is no "correct" behaviour. The mechanisms by which you ask a parser to do DTD or schema validation are entirely dependent on the parser. It's always struck me that Altova's practice of doing schema validation if there is a schemaLocation attribute is not especially useful. If you want to do both DTD and schema validation then one way to achieve this is to have the parser do DTD validation and then filter through an independent schema validator such as Saxon, which works independently of the parser. – Michael Kay Mar 14 '18 at 09:18
  • 1
    Related (but more about purpose than method): [**Why use both XSD and DTD for XML?**](https://stackoverflow.com/q/38727905/290085). – kjhughes Mar 19 '18 at 15:15
  • 1
    @kjhughes - yes the reason you give is pretty much why I want to mix them in the first place. Reassuring to know I'm not the only person who has dreamt up this valid use-case. – Phil Mar 19 '18 at 15:20

1 Answers1

1

I think Michael's comment above provides good insight.

To round-up, XMLSpy have got back to me and confirmed that in XMLSpy2018sp1, any specified XSD will "win" over a DTD with regards to element and attribute content. There is no current workaround other than to temporarily remove the XSD specification from the XML.

They have put it on their wishlist to be able to control both type and order of validation - but don't hold you're breath, as I accept it's a bit of an edge case. I'd have thought it was an easy feature to add tho!

Phil
  • 592
  • 6
  • 15