开发者

XML Namespaces and DTD validation

开发者 https://www.devze.com 2023-01-23 08:22 出处:网络
I making some documents in xml and dtd. I use in xml html namespace to insert image. But I can vali开发者_如何学编程de my document with xmllint, and I don\'t know why :/ validator stops on first line.

I making some documents in xml and dtd. I use in xml html namespace to insert image. But I can vali开发者_如何学编程de 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 file:

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


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.

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

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">

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

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>

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号