XML DTD验证错误

时间:2013-01-09 18:29:50

标签: xml validation dtd

在线验证: http://xmlvalidator.new-studio.org

XML Document (也显示在下方)

问题1

为什么显示以下6个错误?

  1. 第29行第18列:类型ID的属性值“1”必须是名称。
  2. 第34行第22列:必须为元素类型“dob”声明属性“type”。
  3. 第39行第26列:必须为元素类型“address”声明属性“type”。
  4. 第49行第18列:ID类型的属性值“2”必须是名称。
  5. 第54行第22列:必须为元素类型“dob”声明属性“type”。
  6. 第59行第27列:必须为元素类型“address”声明属性“type”。
  7. 问题2

    当我取消注释以下行时没有。 4

    <!ELEMENT persona (name,dob?,address*) >
    

    并评论以下行号。 5

    <!ELEMENT name (first_name,last_name) >
    

    为什么会出现以下错误?

    • 第4行第28列:元素类型“persona”的声明中需要元素类型。

    带有嵌入式DTD的XML文档

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE personality [
    <!ELEMENT personality (persona*) >
    <!-- <!ELEMENT persona (#PCDATA|(name,dob?,address*))> -->
    <!ELEMENT persona (name,dob?,address*) >
    <!ELEMENT name (first_name,last_name) >
    <!ELEMENT first_name (#PCDATA) >
    <!ELEMENT last_name (#PCDATA) >
    <!ELEMENT dob (date+,month+,year+) >
    <!ELEMENT date (#PCDATA) >
    <!ELEMENT month (#PCDATA) >
    <!ELEMENT year (#PCDATA) >
    <!ELEMENT address (building,street,city,state,country,country_code) >
    <!ELEMENT building (#PCDATA) >
    <!ELEMENT street (#PCDATA) >
    <!ELEMENT city (#PCDATA) >
    <!ELEMENT state (#PCDATA) >
    <!ELEMENT country (#PCDATA) >
    <!ELEMENT country_code (#PCDATA) >
    
    <!ATTLIST persona id ID #REQUIRED >
    <!ATTLIST name type (string|number) "string" >
    
    ]>
    
    <?xml-stylesheet type="text/css" href="xmlstyle.css" ?>
    
    <personality>
        <persona id="1">
            <name type="string">
                <first_name>Abhisek</first_name>
                <last_name>Pattnaik</last_name>
            </name>
            <dob type="number">
                <date>29</date>
                <month>8</month>
                <year>1990</year>
            </dob>
            <address type="string">
                <building>Plot-471</building>
                <street>Sahid Nagar</street>
                <city>Bhubaneswar</city>
                <state>Odisha</state>
                <country>India</country>
                <country_code>91</country_code>
            </address>
        </persona>
    
        <persona id="2">
                <name type="string">
                    <first_name>Anindita</first_name>
                    <last_name>Patnaik</last_name>
                </name>
            <dob type="number">
                <date>5</date>
                <month>12</month>
                <year>1996</year>
            </dob>
                <address type="string">
                <building>Plot-471</building>
                <street>Sahid Nagar</street>
                <city>Bhubaneswar</city>
                <state>Odisha</state>
                <country>India</country>
                <country_code>91</country_code>
            </address>
        </persona>
    </personality>
    

1 个答案:

答案 0 :(得分:3)

Line 29 Column 18 : Attribute value "1" of type ID must be a name.
Line 49 Column 18 : Attribute value "2" of type ID must be a name.

ID类型属性的值不能以数字开头。它必须与XML规范中的Name生成匹配,后者排除了初始数字。


Line 34 Column 22 : Attribute "type" must be declared for element type "dob".
Line 39 Column 26 : Attribute "type" must be declared for element type "address".

您尚未为typedob元素声明address属性。


<!ELEMENT persona (#PCDATA|(name,dob?,address*))>

这(在您上面的DTD中注释掉)是非法声明。以下情况可以(但可能不是您想要的):

<!ELEMENT persona (#PCDATA|name|dob|address)*>

有关“混合内容”限制的信息,请参阅