1

当我使用 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>

不知道哪里出了问题。

4

1 回答 1

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>
于 2013-04-08T03:05:05.297 回答