XML命名空间和DTD验证 [英] XML Namespaces and DTD validation

查看:80
本文介绍了XML命名空间和DTD验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I making some documents in xml and dtd. I use in xml html namespace to insert image. But I can valide my document with xmllint, and I don't know why :/ validator stops on first line. XML file:

<?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) >

推荐答案

如果您需要命名空间,则真的应该使用模式作为开始( RelaxNG ).DTD不支持命名空间.可以将它们添加到它们中,但这确实是一个hack,您需要格外小心才能使其正常工作.

If you need namespaces you really should be using a schema for a start (either W3C Schema or RelaxNG). Namespaces are not supported by DTDs. They can be added to them but it really is a hack and you need to take a great deal of care in order to make them work.

现在,您的第一个问题可能是DTD中存在大量错误.这是带有一些评论的更正版本.这仍然不是可以正确使用命名空间的DTD,但是我们会做到这一点:

Now, your first problem is probably the large number of errors in your DTD. Here's a corrected version with some comments. This still isn't a DTD that is going to work with namespaces correctly but we'll get to that:

<!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中:

Now, since DTDs do not have any concept of namespace you need to declare that namespace as an attribute. We can add that to the DTD as attribute of your catalog element by adding:

<!ATTLIST catalog xmlns:html CDATA #FIXED "http://www.w3.org/1999/xhtml">

完成后,我们需要删除几个前缀.首先,不需要在catalog元素上具有前缀,以便可以从DTD中提取出来:

Having done that we need to remove a couple of prefixes. Firstly, there is no need to have the prefix on the catalog element so that can come out from the DTD:

<!ELEMENT catalog (catalogDescription,games)>    

您不是(希望)尝试将目录元素添加到XHTML,而是尝试将XHTML的一部分添加到目录中.因此,您的XML文档现在可以重写为:

You aren't (I hope) trying to add your catalog element to XHTML, you are trying to add part of XHTML to your catalog. So, your XML document can now be rewritten as:

<?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 属性作为开始).

That now validates the initial part of the document (if not all of it) and probably does rather more of what you wanted in the first place. Your DTD is still incomplete so it won't validate (you need to declare the age attribute for a start).

重要的是要意识到您还没有创建一个名称空间感知的DTD-您已经创建了一个DTD,其中某些元素的名称中包含冒号,但在某些方面是无效的.我强烈建议您使用架构而不是DTD.您将完全了解名称空间,并且可以轻松地从XHTML架构文件中导入定义.

It's important though to realise that you haven't created a namespace aware DTD - you have created a DTD in which some elements contain colons in their names which isn't invalid in some ways. I would very strongly suggest that you use a schema rather than a DTD. You will get full namespace awareness and you will be able to simply import definitions from an XHTML schema file.

这篇关于XML命名空间和DTD验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆