验证XML和DTD时出错

时间:2015-03-11 18:35:40

标签: xml dtd xml-validation xml-dtd

我正在处理XML文件,但我遇到了问题。

这是DTD:

<!ELEMENT book (bookinfo,chapter*)>
<!ELEMENT chapter (title,section*)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT bookinfo (title,author,copyright)>
<!ELEMENT author (firstname,surname)>
<!ELEMENT copyright (year,holder)>
<!ENTITY % divers "para|programlisting|itemizedlist|orderedlist">
<!ELEMENT section (title,(%divers;)+)>
<!ELEMENT para (#PCDATA)>
<!ELEMENT programlisting (#PCDATA)>
<!ELEMENT holder (#PCDATA)>
<!ELEMENT surname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT itemizedlist (listitem+)>
<!ELEMENT orderedlist (listitem+)>
<!ELEMENT listitem (%divers;)+>

这是我到目前为止所做的事情(此部分验证还可以)

<?xml version="1.0" encoding='ISO-8859-1' standalone="no"?>
<!DOCTYPE book SYSTEM "simpledocbook.dtd">
<?xml-stylesheet href="docbook.xml" type="application/xml"?>
<book>
<bookinfo>
<title></title>
<author>
<firstname></firstname>
<surname></surname>
</author>
<copyright>
<year></year>
<holder></holder>
</copyright>
</bookinfo>
<chapter>
<title></title>
<section>
<title></title>
<para></para>
<programlisting></programlisting>
</section>
</chapter>
<chapter>
<title></title>
<section>
<title></title>
<para></para>
<programlisting></programlisting>
<itemizedlist></itemizedlist>
<orderedlist></orderedlist>
</section>
</chapter>
</book>

当我尝试添加项目列表时出现问题,我不知道应该将其放在代码中的哪个位置?我试图把它放在某处我得到的所有错误。

你能帮助我找到它为什么不起作用吗? 非常感谢!

1 个答案:

答案 0 :(得分:1)

  

这是我到目前为止所做的事情(此部分验证还可以)

该部分的验证不应该没问题。您应该收到有关itemizedlistorderedlist不完整的错误消息。它们至少需要1 listitem(这听起来像你正在尝试添加)。

这是一个有效的修改版本:

<!DOCTYPE book SYSTEM "simpledocbook.dtd">
<?xml-stylesheet href="docbook.xml" type="application/xml"?>
<book>
    <bookinfo>
        <title></title>
        <author>
            <firstname></firstname>
            <surname></surname>
        </author>
        <copyright>
            <year></year>
            <holder></holder>
        </copyright>
    </bookinfo>
    <chapter>
        <title></title>
        <section>
            <title></title>
            <para></para>
            <programlisting></programlisting>
        </section>
    </chapter>
    <chapter>
        <title></title>
        <section>
            <title></title>
            <para></para>
            <programlisting></programlisting>
            <itemizedlist>
                <listitem>
                    <para/>
                </listitem>
            </itemizedlist>
            <orderedlist>
                <listitem>
                    <para/>
                </listitem>
            </orderedlist>
        </section>
    </chapter>
</book>

我向para添加了空listitem元素,因为至少需要以下一项:paraprogramlisting,{{1} }或itemizedlist