Search code examples
xmlschemadtd

Can an XML document follow both a DTD and an XML Schema?


Is it legal for an XML document to specify that it follows both a DTD and a Schema? Won't the two conflict with one another?


Solution

  • Technically I think you would have problems with the DTD not recognizing the attributes for referencing the schema (the namespace declaration and the schema location).

    However I think it depends on how you're validating your XML and whether or not you can ignore the DTD for validation if a schema is specified.

    Also, for your assignment are you sure you have to reference both from the same XML instance? Maybe you could have 2 versions of the XML; one that references the DTD and one that references the schema?


    Here's two other possible options...

    Declaring the schema attributes:

    <!DOCTYPE doc [
    <!ELEMENT doc (test)>
    <!ATTLIST doc
              xmlns:xsi CDATA #IMPLIED
              xsi:noNamespaceSchemaLocation CDATA #IMPLIED>
    <!ELEMENT test (#PCDATA)>
    ]>
    <doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="test.xsd">
        <test>Test Doc</test>
    </doc>
    

    Using a processing instruction to reference the schema:

    <!DOCTYPE doc [
    <!ELEMENT doc (test)>
    <!ELEMENT test (#PCDATA)>
    ]>
    <?xml-model href="test.xsd"?>
    <doc>
        <test>Test Doc</test>
    </doc>