XML DTD验证:声明子集中不允许使用的字符

时间:2016-10-31 15:44:24

标签: xml dtd xml-validation

尽管存在根元素并且已经在DTD的ATTLIST中插入了所有属性,但是我在w3验证器上出现了几个错误:

  

错误第17行,第9列:声明中不允许使用字符“g”   子集gender CDATA #REQUIRED>

     

错误第21行,第45栏:没有属性“性别”    ...... <person user="WhozYourDaddy" gender="male" email="whozyourdaddy@gmail.com">

     

错误第21行,第58栏:没有属性“email”... <person user="WhozYourDaddy" gender="male" email="whozyourdaddy@gmail.com">

     

错误第29行,第22列:文档类型不允许使用元素   “lastdownload”在这里           <lastdownload> PokemonGo </lastdownload>

     

错误第30行,第19列:文档类型不允许使用元素   “lastlogin”在这里           <lastlogin> 12.03.2016 </lastlogin>

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (person+)>
<!ELEMENT person (firstname,lastname,telephone,city,address, preferences, newsletter, downloads)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT telephone (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT preferences (#PCDATA)>
<!ELEMENT newsletter (#PCDATA)>
<!ELEMENT lastdownload (#PCDATA)>
<!ELEMENT lastlogin (#PCDATA)>
<!ELEMENT downloads (#PCDATA)>
<!ATTLIST person 
        user CDATA #REQUIRED>
        gender CDATA #REQUIRED>
        email CDATA #REQUIRED>
]>
<catalog>
        <person user="WhozYourDaddy" gender="male" email="whozyourdaddy@gmail.com">
        <firstname> X </firstname>
        <lastname> XXXX </lastname>
        <telephone> 06666666 </telephone>
        <city> ZZZZZ </city>
        <address> Azxxxxxh xxx </address>
        <preferences> Educational </preferences>
        <newsletter> Yes </newsletter>
        <lastdownload> PokemonGo </lastdownload>
        <lastlogin> 12.03.2016 </lastlogin>
        <downloads> 5 </downloads>
    </person>
</catalog>

1 个答案:

答案 0 :(得分:1)

要解决几个错误:

  1. 删除>
  2. 中的额外ATTLIST个字符
  3. lastdownloadlastlogin添加到catalog的内容模型。
  4. 以下是适用所有修补程序的XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE catalog [
    <!ELEMENT catalog (person+)>
    <!ELEMENT person (firstname,lastname,telephone,city,address, preferences, 
                      newsletter, lastdownload, lastlogin, downloads)>
    <!ELEMENT firstname (#PCDATA)>
    <!ELEMENT lastname (#PCDATA)>
    <!ELEMENT telephone (#PCDATA)>
    <!ELEMENT city (#PCDATA)>
    <!ELEMENT address (#PCDATA)>
    <!ELEMENT preferences (#PCDATA)>
    <!ELEMENT newsletter (#PCDATA)>
    <!ELEMENT lastdownload (#PCDATA)>
    <!ELEMENT lastlogin (#PCDATA)>
    <!ELEMENT downloads (#PCDATA)>
    <!ATTLIST person 
            user CDATA #REQUIRED
            gender CDATA #REQUIRED
            email CDATA #REQUIRED>
    ]>
    <catalog>
      <person user="WhozYourDaddy" gender="male" email="whozyourdaddy@gmail.com">
        <firstname> X </firstname>
        <lastname> XXXX </lastname>
        <telephone> 06666666 </telephone>
        <city> ZZZZZ </city>
        <address> Azxxxxxh xxx </address>
        <preferences> Educational </preferences>
        <newsletter> Yes </newsletter>
        <lastdownload> PokemonGo </lastdownload>
        <lastlogin> 12.03.2016 </lastlogin>
        <downloads> 5 </downloads>
      </person>
    </catalog>