如何在具有多个级别时根据 DTD 验证 XML 文档?

How to validate XML document against DTD while having multiple levels?

我知道如何根据单级 DTD 验证 XML 文档,但是,当数据嵌套(多级)时我们如何做到这一点。根是 chapters,在根下我有两个主要元素 chapter1chapter2,在每个元素下我都有子元素。这些子元素的名称在 chapter1chapter2 中是相同的。我的问题是:我是否需要在每个 chapter1chapter2 声明下按顺序声明一次或两次 <!ELEMENT mainTopic CDATA #REQUIRED >

<chapters>
    <chapter1>
        <mainTopic>HTML MARKUP AND DOCUMENT STRUCTURE</mainTopic>
        <subTopic1>The Basics of Markup</subTopic1>
        <subTopic2>Anatomy of an HTML Document</subTopic2>
        <subTopic3>The Document Object Model</subTopic3>
    </chapter1>
    <chapter2>
        <mainTopic>HOW CSS WORKS</mainTopic>
        <subTopic1>The Anatomy of CSS Rule</subTopic1>
        <subTopic2>Contexual Selectors</subTopic2>
        <subTopic3>Specialized Contextual Selectors</subTopic3>
        <subTopic4>IDs and Classes</subTopic4>
        <subTopic5>Attibute Selectors</subTopic5>
        <subTopic6>Pseduo-Classes</subTopic6>
        <subTopic7>Pseduo-Elements</subTopic7>
        <subTopic8>Inheritance</subTopic8>
        <subTopic9>The Cascade</subTopic9>
        <subTopic10>Rule Declaration</subTopic10>
    </chapter2>
</chapters>

以下是内联 DTD 代码:

<!DOCTYPE chapters [
    <!ELEMENT chapters (chapter1, chapter2)>

    <!ELEMENT chapters (chapter1, chapter2) >
    <!ELEMENT chapter1 (mainTopic, subTopic1, subTopic2, subTopic3) >

    <!ELEMENT chapter2 (mainTopic, subTopic1, subTopic2, subTopic3, subTopic4, subTopic5, subTopic6, subTopic7, subTopic8, subTopic9, subTopic10) >
    <!ELEMENT mainTopic (#PCDATA) >
    <!ELEMENT subTopic1 (#PCDATA) >
    <!ELEMENT subTopic2 (#PCDATA) >
    <!ELEMENT subTopic3 (#PCDATA) >
    <!ELEMENT subTopic4 (#PCDATA) >
    <!ELEMENT subTopic5 (#PCDATA) >
    <!ELEMENT subTopic6 (#PCDATA) >
    <!ELEMENT subTopic7 (#PCDATA) >
    <!ELEMENT subTopic8 (#PCDATA) >
    <!ELEMENT subTopic9 (#PCDATA) >
    <!ELEMENT subTopic10 (#PCDATA) >
]>

Do I need to declare the <!ELEMENT mainTopic CDATA #REQUIRED > once or twice in order, under each chapter1 and chapter2 declaration?

您只能声明一次元素。您在示例 DTD 中对 mainTopic 的声明是正确的。 (上面引用中的声明不是。)

如果删除 chapters...

的第二个声明,则您的 DTD 和实例有效
<!DOCTYPE chapters [
    <!ELEMENT chapters (chapter1, chapter2)>

    <!ELEMENT chapter1 (mainTopic, subTopic1, subTopic2, subTopic3) >

    <!ELEMENT chapter2 (mainTopic, subTopic1, subTopic2, subTopic3, subTopic4, subTopic5, subTopic6, subTopic7, subTopic8, subTopic9, subTopic10) >
    <!ELEMENT mainTopic (#PCDATA) >
    <!ELEMENT subTopic1 (#PCDATA) >
    <!ELEMENT subTopic2 (#PCDATA) >
    <!ELEMENT subTopic3 (#PCDATA) >
    <!ELEMENT subTopic4 (#PCDATA) >
    <!ELEMENT subTopic5 (#PCDATA) >
    <!ELEMENT subTopic6 (#PCDATA) >
    <!ELEMENT subTopic7 (#PCDATA) >
    <!ELEMENT subTopic8 (#PCDATA) >
    <!ELEMENT subTopic9 (#PCDATA) >
    <!ELEMENT subTopic10 (#PCDATA) >
]>

题外话;如果您可以控制 DTD 和 XML,我强烈建议不要声明单独的 chapter#subTopic# 元素(带有尾随数字)。这样做没有任何好处。我只会声明单个 chaptersubTopic 元素...

<!DOCTYPE chapters [
<!ELEMENT chapters (chapter+)>
<!ELEMENT chapter (mainTopic, subTopic+)>
<!ELEMENT mainTopic (#PCDATA)>
<!ELEMENT subTopic (#PCDATA)>
]>
<chapters>
    <chapter>
        <mainTopic>HTML MARKUP AND DOCUMENT STRUCTURE</mainTopic>
        <subTopic>The Basics of Markup</subTopic>
        <subTopic>Anatomy of an HTML Document</subTopic>
        <subTopic>The Document Object Model</subTopic>
    </chapter>
    <chapter>
        <mainTopic>HOW CSS WORKS</mainTopic>
        <subTopic>The Anatomy of CSS Rule</subTopic>
        <subTopic>Contexual Selectors</subTopic>
        <subTopic>Specialized Contextual Selectors</subTopic>
        <subTopic>IDs and Classes</subTopic>
        <subTopic>Attibute Selectors</subTopic>
        <subTopic>Pseduo-Classes</subTopic>
        <subTopic>Pseduo-Elements</subTopic>
        <subTopic>Inheritance</subTopic>
        <subTopic>The Cascade</subTopic>
        <subTopic>Rule Declaration</subTopic>
    </chapter>
</chapters>