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. That means those names have to be declared, with their prefixes, in the DTD.

Here's an example. I'll start with the easy case in which 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, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>     .     .     .         </ORDERS>     </CUSTOMER> </DOCUMENT> 

Some validating XML processors aren't going to understand the xmlns attribute that you use to declare a namespace. That means that you must declare the attribute as follows . 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, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>     .     .     .         </ORDERS>     </CUSTOMER> </DOCUMENT> 

Now I'm free to use the xmlns attribute in the root element, as I did earlier. 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. For instance, in this next example, 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, 2003</DATE>         <ORDERS>             <ITEM>                 <PRODUCT>Tomatoes</PRODUCT>                 <NUMBER>8</NUMBER>                 <PRICE>.25</PRICE>             </ITEM>     .     .     .         </ORDERS>     </CUSTOMER> </DOCUMENT> 

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

Listing ch03_11.xml
 <?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, 2003</doc:DATE>         <doc:ORDERS>             <doc:ITEM>                 <doc:PRODUCT>Tomatoes</doc:PRODUCT>                 <doc:NUMBER>8</doc:NUMBER>                 <doc:PRICE>.25</doc:PRICE>             </doc:ITEM>             <doc:ITEM>                 <doc:PRODUCT>Oranges</doc:PRODUCT>                 <doc:NUMBER>24</doc:NUMBER>                 <doc:PRICE>.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, 2003</doc:DATE>         <doc:ORDERS>             <doc:ITEM>                 <doc:PRODUCT>Bread</doc:PRODUCT>                 <doc:NUMBER>12</doc:NUMBER>                 <doc:PRICE>.95</doc:PRICE>             </doc:ITEM>             <doc:ITEM>                 <doc:PRODUCT>Apples</doc:PRODUCT>                 <doc:NUMBER>6</doc:NUMBER>                 <doc:PRICE>.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, 2003</doc:DATE>         <doc:ORDERS>             <doc:ITEM>                 <doc:PRODUCT>Asparagus</doc:PRODUCT>                 <doc:NUMBER>12</doc:NUMBER>                 <doc:PRICE>.95</doc:PRICE>             </doc:ITEM>             <doc:ITEM>                 <doc:PRODUCT>Lettuce</doc:PRODUCT>                 <doc:NUMBER>6</doc:NUMBER>                 <doc:PRICE>.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.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net