CodyBugstein
CodyBugstein

Reputation: 23302

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?

Upvotes: 3

Views: 2420

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

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>

Upvotes: 2

Quentin
Quentin

Reputation: 943118

Is it legal for an XML document to specify that it follows both a DTD and a Schema?

Yes

Won't the two conflict with one another?

Only if one of them mandates something the other forbids (in which case claiming to follow both would be a strange thing to do).

Upvotes: 1

Related Questions