XML

Namespaces

XML namespaces offer a way to create names in an XML document that are identified by a Uniform Resource Identifier (URI). By using namespaces in your XML document, you can uniquely identify elements or attributes in the document. For example, consider the following XML document fragment:

  <order>
     <type ordertype="New"/>
     <customer>
        <type customertype="Rich"/>
     </customer>
  </order>

This is a valid XML document, yet it would be difficult for an application reading this document to differentiate between the type element associated with the order element and the type element associated with the customer element. Although in this instance you could change the names of the elements in the DTD and XML document, this solution is not always possible.

Let's look more closely at how these element name problems can occur. For the most part, you want the DTDs you create to be modular, meaning that they contain definitions for only one type of information. These modular DTDs can then be combined to create more complex DTDs.

NOTE
Schemas are like DTDs, except they use a different syntax than DTDs and use XML to define the structure of a set of XML documents. For a detailed discussion of schemas, see Chapter 7.

For example, you could have DTDs that define the structure of the following XML documents: Customer, Order, Order Details, Product, and Category. These DTDs can be used to create Customer, Order, Order Details, Product, and Category XML documents. You can also combine these DTDs to create a single, complete order XML DTD that contains all the detailed information about an order, including the customer, order, order details, product, and category information. The most common problem that occurs when you combine DTDs is naming conflicts between elements.

If we defined the order entity in an external file named Order.dtd and the customer entity in an external file named Customer.dtd, our example order DTD might look like this:

    <?xml version="1.0"?>
    <!--Order -->
    <!ENTITY order SYSTEM "Order.dtd">
    <!--Customer -->
    <!ENTITY customer SYSTEM "Customer.dtd">
    

When the Order and Customer entities are replaced, the DTD might look like this:

    <?xml version="1.0"?>
    <!--Order -->
    <!ELEMENT order (id, customer)>
    <!ELEMENT type()>
    <!ATTRIBUTE type ordertype CDATA "Old">
    <!--Customer -->
    <!ELEMENT customer>
    <!ELEMENT type()>
    <!ATTRIBUTE type customertype CDATA "Rich">

This DTD is invalid because the type element is declared twice with two different types of attributes. This repetition is one of the potential problems of using external entities.

Namespaces enable you to solve this problem. Namespaces are defined within the elements in your XML document. If an element has child elements, the child elements can either inherit the parent's namespace or override the parent's namespace.

by BrainBellupdated
Advertisement: