gpt4 book ai didi

XML 命名空间和 DTD 验证

转载 作者:数据小太阳 更新时间:2023-10-29 02:27:58 28 4
gpt4 key购买 nike

我用 xml 和 dtd 制作了一些文档。我在 xml html 命名空间中使用插入图像。但我可以使用 xmllint 验证我的文档,但我不知道为什么 :/验证程序在第一行停止。XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html: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 文件:

<!ELEMENT html:catalog (catalogDescription,games)>
<!ELEMENT catalogDescription (authors?,catalogInfo?)>
<!ELEMENT authors (author+)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT catalogInfo (#PCDATA)>



<!ELEMENT games (genres,game+)>
<!ELEMENT genres (genreType) #REQUIRED>
<!ATTLIST genreType id ID #REQUIRED>
<!ELEMENT game (title,more)>
<!ATTLIST game lang #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+)>
<!ELEMENT platform>

<!ELEMENT screen (thumbnail,bigimage)>
<!ELEMENT thumbnaul (html:img)>
<!ELEMENT html:img #EMPTY>
<!ATTLIST html:img src CDATA>
<!ELEMENT bigimage (html:img)>
<!ELEMENT available (#PCDATA) >

最佳答案

如果您需要 namespace ,您真的应该首先使用架构(W3C SchemaRelaxNG )。 DTD 不支持命名空间。可以将它们添加到其中,但这确实是一种 hack,您需要格外小心才能使它们正常工作。

现在,您的第一个问题可能是 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 没有任何 namespace 的概念,您需要将该 namespace 声明为属性。我们可以将其添加到 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 架构文件中导入定义。

关于XML 命名空间和 DTD 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4080069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com