Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

xsd - Is it possible to use XML Schemas internally, just like DTDs?

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>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, although you may find some 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...