XML Invalid DTD error

丶灬走出姿态 提交于 2020-01-24 14:16:07

问题


.

Validate Online: http://xmlvalidator.new-studio.org

XML Document (also displayed below)

Question 1

When I uncomment the following line no. 4

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

AND comment the following line no. 5

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

Why the following error shows?

  • Line 4 Column 28 : An element type is required in the declaration of element type "persona".

Here, I want that user can EITHER use parsable character string OR can write the name, dob & address tags and write the info in them.

Question 2

When I replace the encoding from "ISO-8859-1" to "utf-8", and load the page in browser (Google Chrome); it shows the following error.

error on line 2 at column 21: Encoding error

But validation is successful & OK.

Why does it show that error? Which encoding is good?

The XML Document with embedded DTD

<?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 (#PCDATA|(first_name,last_name)) > -->
<!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" >
<!ATTLIST dob type (string|number) "number" >
<!ATTLIST address type (string|number) "string" >

<!ENTITY author "Miguel Pitts" >
<!ENTITY website "example.com" >
<!ENTITY email "miguelpitts4MN@teleosaurs.xyz" >
<!ENTITY copyright "Copyright © Miguel Pitts" >

]>

<?xml-stylesheet type="text/css" href="xmlstyle.css" ?>

<personality>
    <persona id="p1">
        <name type="string">
            <first_name>Miguel</first_name>
            <last_name>Pitts</last_name>
        </name>
        <dob type="number">
            <date>12</date>
            <month>02</month>
            <year>1989</year>
        </dob>
        <address type="string">
            <building>1233</building>
            <street>15</street>
            <city>NY</city>
            <state>Bangkok</state>
            <country>China</country>
            <country_code>82</country_code>
        </address>
    </persona>
</personality>

回答1:


Here, I want that user can EITHER use parsable character string OR can write the name, dob & address tags and write the info in them.

Understandable, but not allowed by the XML spec (for the reasons outlined by Nic Gibson in another SO question). Your best bet is to redesign your XML slightly; you might, for example, declare

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

or you could declare

<!ELEMENT persona_simple (#PCDATA) >
<!ELEMENT persona_struct (name,dob?,address*) >

Why does it show that error? Which encoding is good?

Are those trick questions? It shows that error because it detects that the character encoding of the input cannot possibly be the character encoding declared. By "which encoding is good?" I assume you mean "which encoding should I name in my document's encoding declaration?" -- the answer to that question is "the encoding that you use in the document". If you're asking the question, I guess you aren't sure which encoding your document actually uses; judging from the behavior you describe, it looks as if the document is in fact not using UTF-8 but ISO 8859 part 1.




回答2:


change your persona ELEMENT as shown below

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

for your reference



来源:https://stackoverflow.com/questions/14254213/xml-invalid-dtd-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!