使用xmllint进行验证时接收XML错误

时间:2013-04-08 01:48:38

标签: xml dtd xml-validation

当我使用xmllint检查我的DTD文件时,我收到一条错误消息。请参阅下面的输出。我不确定这一切都出错了。

book.dtd:1: parser error : StartTag: invalid element name
<!ELEMENT books_for_sale (book+)>
 ^
book.dtd:1: parser error : Extra content at the end of the document
<!ELEMENT books_for_sale (book+)>
 ^

这是我的DTD文件

<!ELEMENT books_for_sale (book+)>
<!ELEMENT book (book_id, title, author, co_author_name, editor_name, illustrator_name, language, ISBN, publisher, publisher_date, genre, subject, category, file_size, pages, price, payment_method)>
<!ELEMENT book_id (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT co_author_name (#PCDATA)>
<!ELEMENT editor_name (#PCDATA)>
<!ELEMENT illustrator_name (#PCDATA)>
<!ELEMENT language (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT publisher_date (#PCDATA)>
<!ELEMENT genre (#PCDATA)>
<!ELEMENT subject (#PCDATA)>
<!ELEMENT category (#PCDATA)>
<!ELEMENT file_size (#PCDATA)>
<!ELEMENT pages (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT payment_method (#PCDATA)>

不确定html和xml的组合是否正确完成。尝试使用命名空间来保持这一点。但这对我来说都是新的,所以我不确定它是否写得正确。

<?xml version="1.0"  encoding="utf-8"?>

<!DOCTYPE books_for_sale SYSTEM "book.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:BO="http://resumator3000.com">

<!-- list book inventory -->

<head>
<title>Book Order</title>
<link rel="stylesheet" type="text/css" href="book.css"></link>
</head>
<body>

<div class="page-wrap">

<h3>Books for Sale</h3>
<h4>Great Prices! Limted Selection! No Delivery!</h4>

<!-- present books for sale -->
<BO:books_for_sale>

    <BO:book>
        <p>Book ID: <BO:book_id>BK12345</BO:book_id></p>
        <p>Book Title: <BO:title>Gray Geese Crying</BO:title></p>
        <p>Author: <BO:author>Bill Nedham</BO:author></p>
        <p>Co-Author: <BO:co_author_name>Surely Joking</BO:co_author_name></p>
        <p>Editor: <BO:editor_name>Fried Brian</BO:editor_name></p>
        <p>Illustrator: <BO:illustrator_name>Drew Sumthin</BO:illustrator_name></p>
        <p>Language: <BO:language>Orangatang</BO:language></p>
        <p>ISBN: <BO:ISBN>9912385748391</BO:ISBN></p>
        <p>Publisher: <BO:publisher>Random Random Homes</BO:publisher></p>
        <p>Publish Date: <BO:publisher_date>2001</BO:publisher_date></p>
        <p>Genre: <BO:genre>Poetry</BO:genre></p>
        <p>Subject: <BO:subject>Human Mind</BO:subject></p>
        <p>Category: <BO:category>Self-Help</BO:category></p>
        <p>File Size: <BO:file_size>122 MB</BO:file_size></p>
        <p>Pages: <BO:pages>3</BO:pages></p>
        <p>Price: <span id="dollar">$</span><BO:price>142.99</BO:price></p>
        <p>Payment Method: <BO:payment_method>Paypal</BO:payment_method></p>
    </BO:book>  

</BO:books_for_sale>
</div>

</body>
</html>

不确定这出错的地方。

1 个答案:

答案 0 :(得分:1)

您的DTD没问题,但您无法直接使用xmllint进行检查;你必须检查一个XML文件。

您的其他文件存在问题。您应该拆分XML,因为您使用的是DTD,所以不要使用命名空间。 (您可以重写DTD,将名称空间声明作为属性处理,将前缀元素作为完整名称处理,但在XML中,名称空间声明不是属性,可以出现在任何元素上,前缀是任意的。因此,这些概念并不适用。转化得很好。)

另一个错误是HTML呈现将忽略任何非标准标记,以便取出整个文档。将数据从XML传输到HTML(甚至是XHTML)时,请确保将要呈现的数据放在HTML标记中。您可以使用XSLT执行此操作。

XML文件......

<?xml version="1.0"  encoding="utf-8"?>
<!DOCTYPE books_for_sale SYSTEM "book.dtd">
<books_for_sale>
    <book>
        <book_id>BK12345</book_id>
        <title>Gray Geese Crying</title>
        <author>Bill Nedham</author>
        <co_author_name>Surely Joking</co_author_name>
        <editor_name>Fried Brian</editor_name>
        <illustrator_name>Drew Sumthin</illustrator_name>
        <language>Orangatang</language>
        <ISBN>9912385748391</ISBN>
        <publisher>Random Random Homes</publisher>
        <publisher_date>2001</publisher_date>
        <genre>Poetry</genre>
        <subject>Human Mind</subject>
        <category>Self-Help</category>
        <file_size>122 MB</file_size>
        <pages>3</pages>
        <price>142.99</price>
        <payment_method>Paypal</payment_method>
    </book>  
</books_for_sale>

因此,当您想要生成HTML时,请使用如下样式表: XSLT文件......

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head>
<title>Book Order</title>
<link rel="stylesheet" type="text/css" href="book.css"></link>
</head>
<body>

<div class="page-wrap">

<h3>Books for Sale</h3>
<h4>Great Prices! Limted Selection! No Delivery!</h4>

<!-- present books for sale -->
      <xsl:for-each select="books_for_sale/book">
        <div>
            <p>Book ID: <xsl:value-of select="book_id" /></p>
            <p>Price: <span id="dollar">$<xsl:value-of select="price" /></span></p>
            <p>Payment Method: <xsl:value-of select="payment_method" /></p>
        </div>
      </xsl:for-each>
    </div>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>