Notepad ++针对本地DTD验证XML

时间:2015-09-27 16:16:29

标签: xml notepad++ dtd

我有一个XML文件,我试图针对DTD进行验证(与XML在同一个本地文件夹中)。 XML检查结构良好,但是每次尝试在记事本++中验证DTD都会导致无法加载DTD PresidentsDTD.dtd"错误。知道我需要做些什么来实现这个目标吗?

XML文件:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE LIST SYSTEM "PresidentsDTD.dtd">

<LIST>
<PRESIDENT id="01" gender="male" name="GeorgeWashington">
    <NAME>
        <FIRST>George</FIRST>
        <MIDDLE />
        <LAST>Washington</LAST>
    </NAME>
    <TERM count="2">
        <TERMSTART>1789</TERMSTART>
        <TERMEND>1797</TERMEND>
        <TERML unit="years"> 8  </TERML>
    </TERM>
    <PARTY>N/A</PARTY>
    <HOMESTATE>Virginia</HOMESTATE>
</PRESIDENT>

<PRESIDENT id="02" gender="male" name="JohnAdams">
    <NAME>
        <FIRST>John</FIRST>
        <MIDDLE />
        <LAST>Adams</LAST>
    </NAME>
    <TERM count="2">    
        <TERMSTART>1797</TERMSTART>
        <TERMEND>1801</TERMEND>
        <TERML unit="years"> 8  </TERML>
    </TERM>
    <PARTY>Federalist</PARTY>
    <HOMESTATE>Massachusetts</HOMESTATE>
</PRESIDENT>

<PRESIDENT id="03" gender="male" name="ThomasJefferson">
    <NAME>
        <FIRST>Thomas</FIRST>
        <MIDDLE />
        <LAST>Jefferson</LAST>
    </NAME>
    <TERM count="2">
        <TERMSTART>1801</TERMSTART>
        <TERMEND>1809</TERMEND>
        <TERML unit="years"> 8  </TERML>
    </TERM>
    <PARTY>Republican-Democrat</PARTY>
    <HOMESTATE>Virginia</HOMESTATE>
</PRESIDENT>

<PRESIDENT id="07" gender="male" name="AndrewJackson">
    <NAME>
        <FIRST>Andrew</FIRST>
        <MIDDLE />
        <LAST>Jackson</LAST>
    </NAME>
    <TERM count="2">
        <TERMSTART>1829</TERMSTART>
        <TERMEND>1837</TERMEND>
        <TERML unit="years"> 8  </TERML>
    </TERM>
    <PARTY>Democrat</PARTY>
    <HOMESTATE>Tennesee</HOMESTATE>
</PRESIDENT>

<PRESIDENT id="09" gender="male" name="WilliamHenryHarrison">
    <NAME>
        <FIRST>William</FIRST>
        <MIDDLE>Henry</MIDDLE>
        <LAST>Harrison</LAST>
    </NAME>
    <TERM count="1">
        <TERMSTART>1841</TERMSTART>
        <TERMEND>1841</TERMEND>
        <TERML unit="months"> 1 </TERML>
    </TERM>
    <PARTY>Whig</PARTY>
    <HOMESTATE>Ohio</HOMESTATE>
</PRESIDENT>

<PRESIDENT id="15" gender="male" name="AbrahamLincoln">
    <NAME>
        <FIRST>Abraham</FIRST>
        <MIDDLE />
        <LAST>Lincoln</LAST>
    </NAME>
    <TERM count="2">
        <TERMSTART>1861</TERMSTART>
        <TERMEND>1865</TERMEND>
        <TERML unit="years"> 4  </TERML>
    </TERM>
    <PARTY>Republican</PARTY>
    <HOMESTATE>Illinois</HOMESTATE>
</PRESIDENT>
</LIST>

我的PresidentsDTD.dtd文件(由于某种原因仍然在第2行返回错误):

<!DOCTYPE LIST[
<!ELEMENT LIST PRESIDENT+>
<!ELEMENT PRESIDENT (NAME, TERM, PARTY, HOMESTATE)>
<!ATTLIST PRESIDENT id ID #REQUIRED>
<!ATTLIST PRESIDENT gender (male | female) #IMPLIED>
<!ATTLIST PRESIDENT name CDATA #IMPLIED>
<!ELEMENT NAME (FIRST, MIDDLE?, LAST)>
<!ELEMENT TERM (TERMSTART, TERMEND, TERMLENGTH)>
<!ATTLIST TERM count CDATA #IMPLIED>
<!ELEMENT FIRST (#PCDATA)>
<!ELEMENT MIDDLE (#PCDATA)>
<!ELEMENT LAST (#PCDATA)>
<!ELEMENT TERMSTART (#PCDATA)>
<!ELEMENT TERMEND (#PCDATA)>
<!ELEMENT TERMLENGTH (#PCDATA)>
<!ATTLIST TERMLENGTH unit (months | years) #IMPLIED>
]>

有没有人有解决方案在本地验证DTD?

1 个答案:

答案 0 :(得分:1)

我编辑了你的XML和.dtd文件。现在XML是有效的。注意,在文件中我添加了对你的错误的评论。 XML文件:

<?xml version="1.0" standalone="no"?>
    <!DOCTYPE LIST SYSTEM "PresidentsDTD.dtd">
    <LIST>
    <PRESIDENT id="ID01" gender="male" name="GeorgeWashington"> <!--id must be unique-->
        <NAME>
            <FIRST>George</FIRST>
            <MIDDLE />
            <LAST>Washington</LAST>
        </NAME>
        <TERM count="2">
            <TERMSTART>1789</TERMSTART>
            <TERMEND>1797</TERMEND>
            <TERMLENGTH unit="years"> 8  </TERMLENGTH> <!--You have <TERML> in XML and <TERMLENGTH> in .dtd-->
        </TERM>
        <PARTY>N/A</PARTY>
        <HOMESTATE>Virginia</HOMESTATE>
    </PRESIDENT>

    <PRESIDENT id="ID02" gender="male" name="JohnAdams">
        <NAME>
            <FIRST>John</FIRST>
            <MIDDLE />
            <LAST>Adams</LAST>
        </NAME>
        <TERM count="2">    
            <TERMSTART>1797</TERMSTART>
            <TERMEND>1801</TERMEND>
            <TERMLENGTH unit="years"> 8  </TERMLENGTH>
        </TERM>
        <PARTY>Federalist</PARTY>
        <HOMESTATE>Massachusetts</HOMESTATE>
    </PRESIDENT>

    <PRESIDENT id="ID03" gender="male" name="ThomasJefferson">
        <NAME>
            <FIRST>Thomas</FIRST>
            <MIDDLE />
            <LAST>Jefferson</LAST>
        </NAME>
        <TERM count="2">
            <TERMSTART>1801</TERMSTART>
            <TERMEND>1809</TERMEND>
            <TERMLENGTH unit="years"> 8  </TERMLENGTH>
        </TERM>
        <PARTY>Republican-Democrat</PARTY>
        <HOMESTATE>Virginia</HOMESTATE>
    </PRESIDENT>

    <PRESIDENT id="ID07" gender="male" name="AndrewJackson">
        <NAME>
            <FIRST>Andrew</FIRST>
            <MIDDLE />
            <LAST>Jackson</LAST>
        </NAME>
        <TERM count="2">
            <TERMSTART>1829</TERMSTART>
            <TERMEND>1837</TERMEND>
            <TERMLENGTH unit="years"> 8  </TERMLENGTH>
        </TERM>
        <PARTY>Democrat</PARTY>
        <HOMESTATE>Tennesee</HOMESTATE>
    </PRESIDENT>

    <PRESIDENT id="ID09" gender="male" name="WilliamHenryHarrison">
        <NAME>
            <FIRST>William</FIRST>
            <MIDDLE>Henry</MIDDLE>
            <LAST>Harrison</LAST>
        </NAME>
        <TERM count="1">
            <TERMSTART>1841</TERMSTART>
            <TERMEND>1841</TERMEND>
            <TERMLENGTH unit="months"> 1 </TERMLENGTH>
        </TERM>
        <PARTY>Whig</PARTY>
        <HOMESTATE>Ohio</HOMESTATE>
    </PRESIDENT>

    <PRESIDENT id="ID15" gender="male" name="AbrahamLincoln">
        <NAME>
            <FIRST>Abraham</FIRST>
            <MIDDLE />
            <LAST>Lincoln</LAST>
        </NAME>
        <TERM count="2">
            <TERMSTART>1861</TERMSTART>
            <TERMEND>1865</TERMEND>
            <TERMLENGTH unit="years"> 4  </TERMLENGTH>
        </TERM>
        <PARTY>Republican</PARTY>
        <HOMESTATE>Illinois</HOMESTATE>
    </PRESIDENT>
    </LIST>

如果您与XML分开创建文件DTD架构,则它不应包含&#34; [&#34; ]&#34;。你没有定义PARTY和HOMESTATE。你在PRESIDENT中定义属性是错误的。
PresidentsDTD.dtd:

<!ELEMENT LIST (PRESIDENT+)>

<!ELEMENT PRESIDENT (NAME, TERM, PARTY, HOMESTATE)>

<!ELEMENT NAME (FIRST, MIDDLE?, LAST)>
<!ELEMENT FIRST (#PCDATA)>
<!ELEMENT MIDDLE (#PCDATA)>
<!ELEMENT LAST (#PCDATA)> 

<!ELEMENT TERM (TERMSTART, TERMEND, TERMLENGTH)>
<!ELEMENT TERMSTART (#PCDATA)>
<!ELEMENT TERMEND (#PCDATA)>
<!ELEMENT TERMLENGTH (#PCDATA)>

<!ELEMENT PARTY (#PCDATA)>
<!ELEMENT HOMESTATE (#PCDATA)>


<!ATTLIST PRESIDENT 
            id ID #REQUIRED 
            gender (male | female) #IMPLIED 
            name CDATA #IMPLIED>
<!ATTLIST TERM 
            count CDATA #IMPLIED>
<!ATTLIST TERMLENGTH 
            unit (months | years) #IMPLIED>