是否可以在内部使用 XML 架构,就像 DTD 一样?
Is it possible to use XML Schemas internally, just like DTDs?
我有以下 XML 文件,其中包含内部 DTD 验证:
<?xml version="1.0"?>
<!DOCTYPE animals [
<!ELEMENT animals (animal)*>
<!ELEMENT animal (skin, noise, eyes, diet, class, weight, special_skill)>
<!ELEMENT skin (#PCDATA)>
<!ELEMENT noise (#PCDATA)>
<!ELEMENT eyes (#PCDATA)>
<!ELEMENT diet (#PCDATA)>
<!ELEMENT class (#PCDATA)>
<!ELEMENT weight (#PCDATA)>
<!ELEMENT special_skill (#PCDATA)>
<!ATTLIST animal name CDATA #REQUIRED >
<!ATTLIST weight unit CDATA "kg">
]>
<animals>
<animal name="cow">
<skin>
Straight fur
</skin>
<noise>
Moo!
</noise>
<eyes>
2
</eyes>
<diet>
Herbivore
</diet>
<class>
Mammalia
</class>
<weight unit="kg">
635-1134
</weight>
<special_skill>
Chewing
</special_skill>
</animal>
<animal name="sheep">
...
</animal>
</animals>
我一直在寻找如何用 XSD 替换 DTD 验证,但找不到任何示例。似乎 XSDs 总是需要在单独的文件中定义并由 XML 引用。 XSD是不是不能内部使用?
不,尽管您可能会找到一些 ad hoc implementational support for embedding an XSD within an XML document instance, there is no standard mechanism for XSD that corresponds to DTD's internal subset。
原则上你可以这样做:
<package>
<xs:schema id="XSD">
... schema goes here ...
</xs:schema>
<doc xsi:noNamespaceSchemaLocation="#XSD">
... instance goes here ...
</doc>
</package>
但是 (a) 不能保证任何特定的模式处理器都会支持这种形式的位置 URI,并且 (b) 很难看出这一点。
我知道这是用 DTD 完成的,但我从来没有真正理解过:
(i) 如果您想验证文档,您肯定想知道它对什么模式有效,而不是相信它会定义自己的模式?知道"this document is valid against some schema but I've no idea what's in that schema"有什么用?
(ii) 你什么时候有过只描述单个实例文档的架构?模式本质上定义了文档的 类,而那些 类 很少是单例。
我有以下 XML 文件,其中包含内部 DTD 验证:
<?xml version="1.0"?>
<!DOCTYPE animals [
<!ELEMENT animals (animal)*>
<!ELEMENT animal (skin, noise, eyes, diet, class, weight, special_skill)>
<!ELEMENT skin (#PCDATA)>
<!ELEMENT noise (#PCDATA)>
<!ELEMENT eyes (#PCDATA)>
<!ELEMENT diet (#PCDATA)>
<!ELEMENT class (#PCDATA)>
<!ELEMENT weight (#PCDATA)>
<!ELEMENT special_skill (#PCDATA)>
<!ATTLIST animal name CDATA #REQUIRED >
<!ATTLIST weight unit CDATA "kg">
]>
<animals>
<animal name="cow">
<skin>
Straight fur
</skin>
<noise>
Moo!
</noise>
<eyes>
2
</eyes>
<diet>
Herbivore
</diet>
<class>
Mammalia
</class>
<weight unit="kg">
635-1134
</weight>
<special_skill>
Chewing
</special_skill>
</animal>
<animal name="sheep">
...
</animal>
</animals>
我一直在寻找如何用 XSD 替换 DTD 验证,但找不到任何示例。似乎 XSDs 总是需要在单独的文件中定义并由 XML 引用。 XSD是不是不能内部使用?
不,尽管您可能会找到一些 ad hoc implementational support for embedding an XSD within an XML document instance, there is no standard mechanism for XSD that corresponds to DTD's internal subset。
原则上你可以这样做:
<package>
<xs:schema id="XSD">
... schema goes here ...
</xs:schema>
<doc xsi:noNamespaceSchemaLocation="#XSD">
... instance goes here ...
</doc>
</package>
但是 (a) 不能保证任何特定的模式处理器都会支持这种形式的位置 URI,并且 (b) 很难看出这一点。
我知道这是用 DTD 完成的,但我从来没有真正理解过:
(i) 如果您想验证文档,您肯定想知道它对什么模式有效,而不是相信它会定义自己的模式?知道"this document is valid against some schema but I've no idea what's in that schema"有什么用?
(ii) 你什么时候有过只描述单个实例文档的架构?模式本质上定义了文档的 类,而那些 类 很少是单例。