如果您需要命名空间,您really应该使用模式作为开始(或者W3C 架构 http://www.w3.org/XML/Schema or RelaxNG http://www.relaxng.org/)。 DTD 不支持命名空间。它们可以被添加到其中,但这确实是一个黑客,你需要非常小心才能让它们工作。
现在,您的第一个问题可能是 DTD 中存在大量错误。这是带有一些评论的更正版本。这仍然不是一个能够正确使用命名空间的 DTD,但我们会做到这一点:
<!ELEMENT html:catalog (catalogDescription,games)>
<!ELEMENT catalogDescription (authors?,catalogInfo?)>
<!ELEMENT authors (author+)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT catalogInfo (#PCDATA)>
<!ELEMENT games (genres,game+)>
<!-- #REQUIRED is not applicable to elements -->
<!ELEMENT genres (genreType)>
<!ATTLIST genreType id ID #REQUIRED>
<!ELEMENT game (title,more)>
<!-- attributes must have a type -->
<!ATTLIST game lang CDATA #IMPLIED>
<!ELEMENT more (screen, description, genre, rank, platforms,cost)>
<!ATTLIST genre ref IDREF #REQUIRED>
<!ELEMENT cost (#PCDATA) >
<!ELEMENT title (#PCDATA)>
<!ELEMENT rank EMPTY>
<!ATTLIST rank points CDATA #REQUIRED>
<!ELEMENT description (#PCDATA)>
<!ELEMENT platforms (platform+)>
<!-- this element doesn't make sense - it must have content of some sort,
I've made it empty but it's your data! -->
<!ELEMENT platform EMPTY>
<!ELEMENT screen (thumbnail,bigimage)>
<!-- I assume that you meant thumbnail -->
<!ELEMENT thumbnail (html:img)>
<!-- that's EMPTY not #EMPTY -->
<!ELEMENT html:img EMPTY>
<!-- the attribute must have the #REQUIRED, #FIXED, etc statement -->
<!ATTLIST html:img src CDATA #REQUIRED>
<!ELEMENT bigimage (html:img)>
<!ELEMENT available (#PCDATA) >
现在,由于 DTD 没有任何名称空间的概念,因此您需要将该名称空间声明为属性。我们可以通过添加以下内容将其作为目录元素的属性添加到 DTD 中:
<!ATTLIST catalog xmlns:html CDATA #FIXED "http://www.w3.org/1999/xhtml">
完成此操作后,我们需要删除几个前缀。首先,目录元素上不需要有前缀,这样就可以从 DTD 中得出:
<!ELEMENT catalog (catalogDescription,games)>
您并不是(我希望)尝试将目录元素添加到 XHTML,而是尝试将 XHTML 的一部分添加到您的目录。因此,您的 XML 文档现在可以重写为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<?xml-stylesheet type="text/css" href="style.css" ?>
<catalog xmlns:html="http://www.w3.org/1999/xhtml">
<catalogDescription>
<authors>Autorzy:
<author age="21">autor1</author>
<author age="21">autor2</author>
</authors>
<catalogInfo>Katalog zawiera spis gier które posiadamy w sprzedaży w naszym sklepie z grami.</catalogInfo>
</catalogDescription>
<games>
<!-- some data-->
</games>
</catalog>
现在,它验证了文档的初始部分(如果不是全部),并且可能会完成您最初想要的更多操作。您的 DTD 仍然不完整,因此它不会验证(您需要声明age
属性作为开始)。
但重要的是要认识到您还没有创建一个命名空间感知的 DTD - 您创建了一个 DTD,其中某些元素的名称中包含冒号,这在某些方面并不是无效的。我强烈建议您使用模式而不是 DTD。您将获得完整的命名空间意识,并且能够简单地从 XHTML 模式文件导入定义。