无法理解XML DTD。解析时获取根元素不正确的错误

时间:2014-07-11 11:05:11

标签: xml validation dtd

我是XML的新手。我正在完成一项任务,而且我仍然坚持使用外部DTD验证。当我解析说根元素格式不正确的文件时,我收到错误。两个文件都在同一目录中。

这是外部DTD:

   <!DOCTYPE atoms [
   <!ELEMENT atoms (hydrogen, carbon, oxygen, titanium, uranium)>
   <!ATTLIST atoms properties CDATA #REQUIRED>
   <!ELEMENT hydrogen (#PCDATA)>
   <!ELEMENT carbon (#PCDATA)>
   <!ELEMENT oxygen (#PCDATA)>
   <!ELEMENT titanium (#PCDATA)>
   <!ELEMENT uranium (#PCDATA)> 
   <!ATTLIST properties symbol CDATA #REQUIRED>
   <!ATTLIST properties name CDATA #REQUIRED>
   <!ATTLIST properties atomicNumber CDATA #REQUIRED>
   <!ATTLIST properties atomicWeight CDATA #REQUIRED>
   ]>

这是我的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--DTD is in a local directory file named atoms.dtd -->
<!-- Week 3 Homework -->
<!DOCTYPE atoms SYSTEM "atoms.dtd">    
  <atoms>
    <hydrogen>   
        <properties symbol="H" name="Hydrogen" atomicNumber="1" atomicWeight="1.00794"/>
    </hydrogen>

    <carbon>   
        <properties symbol="C" name="Carbon" atomicNumber="6" atomicWeight="12.011"/>
    </carbon>

    <oxygen>   
        <properties symbol="O" name="Oxygen" atomicNumber="8" atomicWeight="15.999"/>
    </oxygen>

    <titanium>   
        <properties symbol="Ti" name="Titanium" atomicNumber="22" atomicWeight="47.867"/>
    </titanium>

    <uranium>   
        <properties symbol="U" name="Uranium" atomicNumber="92" atomicWeight="238.03"/>
    </uranium>
  </atoms>

编辑:

我刚刚用这种方式尝试过,当我解析时,我仍然遇到错误。

外部DTD:

    <!DOCTYPE atoms [ <!ELEMENT atoms (atom+)>  
    <!ELEMENT atom (symbol, name, atomicNumber, atomicWeight)>
    <!ELEMENT symbol (#PCDATA)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT atomicNumber (#PCDATA)>
    <!ELEMENT atomicWeight (#PCDATA)> ]>

XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--DTD is in a local directory file named atomsV2.dtd -->
    <!-- Week 3 Homework -->
    <!DOCTYPE atoms SYSTEM "atomsV2.dtd"> 
        <atoms>
            <atom> 
                <symbol H />
                <name Hydrogen />
                <atomicNumber 1 />
                <atomicWeight 1.00794 />
           </atom>

            <atom>   
                <symbol C />
                <name Carbon />
                <atomicNumber 6 />
                <atomicWeight 12.011 />
            </atom>

            <atom>   
                <symbol O />
                <name Oxygen />
                <atomicNumber 8 />
                <atomicWeight 15.999 />
            </atom>

            <atom>   
                <symbol Ti />
                <name Titanium />
                <atomicNumber 22 />
                <atomicWeight 47.867 />
            </atom>

            <atom>   
                <symbol U />
                <name Uranium />
                <atomicNumber 92 />
                <atomicWeight 238.03 />
            </atom>
         </atoms>

1 个答案:

答案 0 :(得分:0)

这是一个有效的版本。您的问题是显示两个结构完全不同的XML / DTD对,因此,如果这不是您正在寻找的答案,请更新您的问题。

此外,请参阅External referenced DTD in XML,以便更好地解释为什么您所做的工作无效。

外部DTD

<!ELEMENT atoms (hydrogen, carbon, oxygen, titanium, uranium)>
<!ATTLIST atoms properties CDATA #IMPLIED>
<!ELEMENT hydrogen (properties)>
<!ELEMENT carbon (properties)>
<!ELEMENT oxygen (properties)>
<!ELEMENT titanium (properties)>
<!ELEMENT uranium (properties)>
<!ELEMENT properties EMPTY>
<!ATTLIST properties 
    symbol CDATA #REQUIRED
    name CDATA #REQUIRED
    atomicNumber CDATA #REQUIRED
    atomicWeight CDATA #REQUIRED>

<强> XML

<!DOCTYPE atoms SYSTEM "atoms.dtd">
<atoms>
    <hydrogen>   
        <properties symbol="H" name="Hydrogen" atomicNumber="1" atomicWeight="1.00794"/>
    </hydrogen>

    <carbon>   
        <properties symbol="C" name="Carbon" atomicNumber="6" atomicWeight="12.011"/>
    </carbon>

    <oxygen>   
        <properties symbol="O" name="Oxygen" atomicNumber="8" atomicWeight="15.999"/>
    </oxygen>

    <titanium>   
        <properties symbol="Ti" name="Titanium" atomicNumber="22" atomicWeight="47.867"/>
    </titanium>

    <uranium>   
        <properties symbol="U" name="Uranium" atomicNumber="92" atomicWeight="238.03"/>
    </uranium>
</atoms>