0

嘿伙计们,我有一个外部的 XML DTD 代码,但是当我尝试验证它时,我似乎得到了一个错误。我的代码如下:

<!DOCTYPE catalog [ 
    <!ELEMENT catalog (book)+>
    <!ELEMENT book (title, authors, year_published, ISBN, number_of_pages, price) >
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT authors (author)>
    <!ELEMENT author (first_name, last_name, gender)>
        <!ELEMENT first_name (#PCDATA)>
        <!ELEMENT last_name (#PCDATA)>
        <!ELEMENT gender (#PCDATA)>
    <!ELEMENT year_pusblished (#PCDATA)>
    <!ELEMENT ISBN (#PCDATA)>
    <!ELEMENT number_of_pages (#PCDATA)>
    <!ELEMENT price (discount)>
        <!ATTLIST price discount (yes|no) "no">]>

我知道一切都是正确的,但是当我验证它时,会出现错误消息:

“文档类型声明中包含或指向的标记声明必须格式正确。第 3 行第 4 列”

我已经检查了所有内容,但似乎无法正常工作。你们能帮忙吗?

4

1 回答 1

0

如果你写入文件 .xml 你没有产生错误,但如果你有一个 .dtd 文件你必须清除元素!正确的文件 test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [ 
<!ELEMENT catalog (book)+>
<!ELEMENT book (title, authors, year_published, ISBN, number_of_pages, price) >
<!ELEMENT title (#PCDATA)>
<!ELEMENT authors (author)>
<!ELEMENT author (first_name, last_name, gender)>
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT year_pusblished (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT number_of_pages (#PCDATA)>
<!ELEMENT price (discount)>
<!ATTLIST price discount (yes|no) "no">]>
<catalog>
    <book>
        <title></title>
        <authors>
            <author>
                <first_name></first_name>
                <last_name></last_name>
                <gender></gender>
            </author>
        </authors>
        <year_published></year_published>
        <ISBN></ISBN>
        <number_of_pages></number_of_pages>
        <price>
            <discount></discount>
        </price>
    </book>
</catalog>

正确的文件 test.dtd (链接到 xml 文件,声明到相同的 test.xml ):

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT catalog (book)+>
<!ELEMENT book (title, authors, year_published, ISBN, number_of_pages, price) >
<!ELEMENT title (#PCDATA)>
<!ELEMENT authors (author)>
<!ELEMENT author (first_name, last_name, gender)>
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT year_pusblished (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT number_of_pages (#PCDATA)>
<!ELEMENT price (discount)>
<!ATTLIST price discount (yes|no) "no">
于 2013-09-24T11:18:43.673 回答