XML

Attributes and Namespaces

An attribute name can be used only once in an element. When you combine a namespace prefix with an attribute name, the combination must be unique. The following is acceptable:

  <?xml version="1.0"?>
  <message xmlns='http://www.northwindtraders.com/bill'
     xmlns:body='http://www.northwindtraders.com/message'>
     <date>1/1/2002</date>
     <body:body body:importance='High' importance='High'>Your current
        balance is $1,999,999.00. Please pay immediately. Thank you.
     </body:body>
  </message>

In this case, the first importance attribute is associated with the body namespace, and the second importance attribute is associated with the default namespace.

Remember, you can define an element name only once in your DTD. Namespaces resolve the element naming conflicts. By using namespaces, you can reference several different documents that all use the same element names.

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.

by BrainBellupdated
Advertisement: