是否可以在内部使用 XML 模式,就像 DTD 一样? [英] Is it possible to use XML Schemas internally, just like DTDs?

查看:15
本文介绍了是否可以在内部使用 XML 模式,就像 DTD 一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下包含内部 DTD 验证的 XML 文件:

I have the following XML file which includes internal DTD validation:

<?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 验证,但找不到任何示例.似乎 XSD 总是需要在单独的文件中定义并由 XML 引用.XSD不能在内部使用是真的吗?

I was looking for how to replace the DTD validation with an XSD but couldn't find any examples. It seems like XSDs always need to be defined in a separate file and the be referenced by the XML. Is this true that XSD cannot be used internally?

推荐答案

原则上可以这样做:

<package>
  <xs:schema id="XSD">
   ... schema goes here ...
  </xs:schema>
  <doc xsi:noNamespaceSchemaLocation="#XSD">
   ... instance goes here ...
  </doc>
</package>

但是 (a) 不能保证任何特定的架构处理器都会支持这种形式的位置 URI,并且 (b) 很难理解这一点.

But (a) there's no guarantee that any particular schema processor will support this form of location URI, and (b) it's hard to see the point.

我知道它是用 DTD 完成的,但我从来没有真正理解过:

I know it's done with DTDs, but I've never really understood:

(i) 如果你想验证一个文档,你肯定想知道它对什么架构有效,而不是相信它来定义自己的架构?知道此文档对某些架构有效,但我不知道该架构中有什么"有什么用?

(i) if you want to validate a document, surely you want to know what schema it's valid against, rather than trusting it to define its own? What use is it to know "this document is valid against some schema but I've no idea what's in that schema"?

(ii) 您什么时候有过只描述单个实例文档的模式?Schema 本质上定义了文档类,而这些类很少是单例的.

(ii) when did you ever have a schema that described only a single instance document? Schemas by their nature define classes of document, and those classes are rarely singletons.

这篇关于是否可以在内部使用 XML 模式,就像 DTD 一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆