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

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

问题描述

我有以下包含内部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进行引用。

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)您什么时候拥有一个仅描述单个实例文档的模式?模式按其性质定义文档的类,而这些类很少是单例。

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

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

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