Namespaces and DTDs

There's one more topic that I want to cover now that we're discussing the basics of creating DTDs—how to use namespaces when you're using DTDs. In fact, this will give us an introduction to the next chapter, where we'll work with declaring attributes as well as elements.

The important thing to recall is that as far as standard XML processors are concerned, namespace prefixes are just text prepended to tag and attribute names with a colon, so they change those tag and attribute names. This means that those names must be declared, with their prefixes, in the DTD.

Here's an example; I'll start with the easy case where I'm using a default namespace like this:

<?xml version = "1.0" standalone="yes"?>
<!DOCTYPE DOCUMENT [
<!ELEMENT DOCUMENT (CUSTOMER)*>
<!ELEMENT CUSTOMER (NAME,DATE,ORDERS)>
<!ELEMENT NAME (LAST_NAME,FIRST_NAME)>
<!ELEMENT LAST_NAME (#PCDATA)>
<!ELEMENT FIRST_NAME (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT ORDERS (ITEM)*>
<!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)>
<!ELEMENT PRODUCT (#PCDATA)>
<!ELEMENT NUMBER (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]>
<DOCUMENT xmlns="http://www.starpowder.com/dtd/">
    <CUSTOMER>
        <NAME>
            <LAST_NAME>Smith</LAST_NAME>
            <FIRST_NAME>Sam</FIRST_NAME>
        </NAME>
        <DATE>October 15, 2001</DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>Tomatoes</PRODUCT>
                <NUMBER>8</NUMBER>
                <PRICE>$1.25</PRICE>
            </ITEM>
    .
    .
    .
            <ITEM>
                <PRODUCT>Asparagus</PRODUCT>
                <NUMBER>12</NUMBER>
                <PRICE>$2.95</PRICE>
            </ITEM>
            <ITEM>
                <PRODUCT>Lettuce</PRODUCT>
                <NUMBER>6</NUMBER>
                <PRICE>$11.50</PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
</DOCUMENT>

Some validating XML processors aren't going to understand the xmlns attribute that you use to declare a namespace. (See the later section "Validating Against a DTD," for details on XML validators.) This means that you must declare that attribute like this; here, I'm using the <!ATTLIST> element (as we'll see how to do in the next chapter) to declare this attribute, indicating that the xmlns attribute has a fixed value, which I'm setting to the namespace identifier, "http://www.starpowder.com/dtd/":

<?xml version = "1.0" standalone="yes"?>
<!DOCTYPE DOCUMENT [
<!ELEMENT DOCUMENT (CUSTOMER)*>
<!ATTLIST DOCUMENT
    xmlns CDATA #FIXED "http://www.starpowder.com/dtd/">
<!ELEMENT CUSTOMER (NAME,DATE,ORDERS)>
<!ELEMENT NAME (LAST_NAME,FIRST_NAME)>
<!ELEMENT LAST_NAME (#PCDATA)>
<!ELEMENT FIRST_NAME (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT ORDERS (ITEM)*>
<!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)>
<!ELEMENT PRODUCT (#PCDATA)>
<!ELEMENT NUMBER (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]>
<DOCUMENT xmlns="http://www.starpowder.com/dtd/">
    <CUSTOMER>
        <NAME>
            <LAST_NAME>Smith</LAST_NAME>
            <FIRST_NAME>Sam</FIRST_NAME>
        </NAME>
        <DATE>October 15, 2001</DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>Tomatoes</PRODUCT>
                <NUMBER>8</NUMBER>
                <PRICE>$1.25</PRICE>
            </ITEM>
    .
    .
    .
            <ITEM>
                <PRODUCT>Asparagus</PRODUCT>
                <NUMBER>12</NUMBER>
                <PRICE>$2.95</PRICE>
            </ITEM>
            <ITEM>
                <PRODUCT>Lettuce</PRODUCT>
                <NUMBER>6</NUMBER>
                <PRICE>$11.50</PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
</DOCUMENT>

Now I'm free to use the xmlns attribute in the root element. That's all there is to setting up a default namespace when using DTDs.

However, if you want to use a namespace prefix throughout a document, the process is a little more involved. In this next example, I use the namespace prefix doc: for the namespace "http://www.starpowder.com/dtd/". To do that, I declare a new attribute, xmlns:doc, and use that attribute in the root element like this to set up the namespace:

<?xml version = "1.0" standalone="yes"?>
<!DOCTYPE DOCUMENT [
<!ELEMENT DOCUMENT (CUSTOMER)*>
<!ATTLIST doc:DOCUMENT
    xmlns:doc CDATA #FIXED "http://www.starpowder.com/dtd/">
<!ELEMENT CUSTOMER (NAME,DATE,ORDERS)>
<!ELEMENT NAME (LAST_NAME,FIRST_NAME)>
<!ELEMENT LAST_NAME (#PCDATA)>
<!ELEMENT FIRST_NAME (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT ORDERS (ITEM)*>
<!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)>
<!ELEMENT PRODUCT (#PCDATA)>
<!ELEMENT NUMBER (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]>
<DOCUMENT xmlns:doc="http://www.starpowder.com/dtd/">
    <CUSTOMER>
        <NAME>
            <LAST_NAME>Smith</LAST_NAME>
            <FIRST_NAME>Sam</FIRST_NAME>
        </NAME>
        <DATE>October 15, 2001</DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>Tomatoes</PRODUCT>
                <NUMBER>8</NUMBER>
                <PRICE>$1.25</PRICE>
            </ITEM>
    .
    .
    .
            <ITEM>
                <PRODUCT>Asparagus</PRODUCT>
                <NUMBER>12</NUMBER>
                <PRICE>$2.95</PRICE>
            </ITEM>
            <ITEM>
                <PRODUCT>Lettuce</PRODUCT>
                <NUMBER>6</NUMBER>
                <PRICE>$11.50</PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>
</DOCUMENT>

Now I can use the doc: prefix throughout the document, where necessary:

<?xml version = "1.0" standalone="yes"?>
<!DOCTYPE doc:DOCUMENT [
<!ELEMENT doc:DOCUMENT (doc:CUSTOMER)*>
<!ATTLIST doc:DOCUMENT
    xmlns:doc CDATA #FIXED "http://www.starpowder.com/dtd/">
<!ELEMENT doc:CUSTOMER (doc:NAME,doc:DATE,doc:ORDERS)>
<!ELEMENT doc:NAME (doc:LAST_NAME,doc:FIRST_NAME)>
<!ELEMENT doc:LAST_NAME (#PCDATA)>
<!ELEMENT doc:FIRST_NAME (#PCDATA)>
<!ELEMENT doc:DATE (#PCDATA)>
<!ELEMENT doc:ORDERS (doc:ITEM)*>
<!ELEMENT doc:ITEM (doc:PRODUCT,doc:NUMBER,doc:PRICE)>
<!ELEMENT doc:PRODUCT (#PCDATA)>
<!ELEMENT doc:NUMBER (#PCDATA)>
<!ELEMENT doc:PRICE (#PCDATA)>
]>
<doc:DOCUMENT xmlns:doc="http://www.starpowder.com/dtd/">
    <doc:CUSTOMER>
        <doc:NAME>
            <doc:LAST_NAME>Smith</doc:LAST_NAME>
            <doc:FIRST_NAME>Sam</doc:FIRST_NAME>
        </doc:NAME>
        <doc:DATE>October 15, 2001</doc:DATE>
        <doc:ORDERS>
            <doc:ITEM>
                <doc:PRODUCT>Tomatoes</doc:PRODUCT>
                <doc:NUMBER>8</doc:NUMBER>
                <doc:PRICE>$1.25</doc:PRICE>
            </doc:ITEM>
            <doc:ITEM>
                <doc:PRODUCT>Oranges</doc:PRODUCT>
                <doc:NUMBER>24</doc:NUMBER>
                <doc:PRICE>$4.98</doc:PRICE>
            </doc:ITEM>
        </doc:ORDERS>
    </doc:CUSTOMER>
    <doc:CUSTOMER>
        <doc:NAME>
            <doc:LAST_NAME>Jones</doc:LAST_NAME>
            <doc:FIRST_NAME>Polly</doc:FIRST_NAME>
        </doc:NAME>
        <doc:DATE>October 20, 2001</doc:DATE>
        <doc:ORDERS>
            <doc:ITEM>
                <doc:PRODUCT>Bread</doc:PRODUCT>
                <doc:NUMBER>12</doc:NUMBER>
                <doc:PRICE>$14.95</doc:PRICE>
            </doc:ITEM>
            <doc:ITEM>
                <doc:PRODUCT>Apples</doc:PRODUCT>
                <doc:NUMBER>6</doc:NUMBER>
                <doc:PRICE>$1.50</doc:PRICE>
            </doc:ITEM>
        </doc:ORDERS>
    </doc:CUSTOMER>
    <doc:CUSTOMER>
        <doc:NAME>
            <doc:LAST_NAME>Weber</doc:LAST_NAME>
            <doc:FIRST_NAME>Bill</doc:FIRST_NAME>
        </doc:NAME>
        <doc:DATE>October 25, 2001</doc:DATE>
        <doc:ORDERS>
            <doc:ITEM>
                <doc:PRODUCT>Asparagus</doc:PRODUCT>
                <doc:NUMBER>12</doc:NUMBER>
                <doc:PRICE>$2.95</doc:PRICE>
            </doc:ITEM>
            <doc:ITEM>
                <doc:PRODUCT>Lettuce</doc:PRODUCT>
                <doc:NUMBER>6</doc:NUMBER>
                <doc:PRICE>$11.50</doc:PRICE>
            </doc:ITEM>
        </doc:ORDERS>
    </doc:CUSTOMER>
</doc:DOCUMENT>

And that's all it takes—now this document, complete with namespace, is valid. This example has introduced us to a very important topic—declaring attributes in DTDs. I'll take a look at how that works in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.151.21