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

爱⌒轻易说出口 提交于 2019-12-10 19:57:13

问题


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?


回答1:


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.xsl"?>
<doc>
    <test>Test Doc</test>
</doc>



回答2:


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).



来源:https://stackoverflow.com/questions/16021048/can-an-xml-document-follow-both-a-dtd-and-an-xml-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!