[Previous] [Contents] [Next]

Declaring Namespaces in DTDs


In the preceding example, we used the tag form <body:body>. You must declare the element using this format in your DTD if a namespace is used. For example, this body element would need to be declared in your DTD as follows:


  <!ELEMENT body:body (#PCDATA)>

If this declaration is used in an external DTD, all documents that reference this external DTD will need to use the name specified in the DTD (in this case, body:body).

You can use entities to enable users to decide whether they want to use a namespace, and if they do, what prefix they want to use. To do so, make the following declarations in your DTD:


  <!ENTITY % ns ''> <!-- Can be overridden in the internal
                        subset of a schema document to establish a
                        namespace prefix -->
  <!ELEMENT %ns;body (#PCDATA)>

If these declarations were included in an external DTD named MessageBody.dtd, you could include the following reference in your internal DTD:


  <?xml version="1.0"?>
  <!DOCTYPE message
  [
  <!ENTITY % ns 'body:'>
  <!ENTITY messagebody SYSTEM "MessageBody.dtd">
  

The local declaration of the ns entity will override the declaration in MessageBody.dtd. Using this technique, each document that uses elements defined in an external DTD can create its own names for the namespace. Let's now move on to the other three specifications related to XML: XPath, XPointer, and XLink.


[Previous] [Contents] [Next]