Section 6.5.  Expectations

JavaScript Editor Free JavaScript Editor     JavaScript Debugger 




Main Page

Previous Page
Next Page

6.5. Expectations

When the Rolling Stones sang "You Can't Always Get What You Want," they were telling only half of the story. The other half is, "You Don't Always Want What You Get." Yeah, it doesn't roll off of the tongue the same way, and I don't sing anything like Mick Jagger; in fact, my children would prefer it if I didn't sing at all. So when I sing, they're both not getting something they want and getting something that they don't want. They'll get over it, but how would XML handle getting something expected and getting something unexpected?

6.5.1. Namespaces

Dealing with both the expected and the unexpected is what namespaces in XML are all about. A namespace is used to describe vocabularies because in some instances the same element name could have two different meanings, which is an unexpected occurrence often with undesirable results.

To put it in nontechnical terms, imagine that you have a shipment of cotton that you want to ship from India to England. Let's say that you want it to be sent on a particular ship that sails in November. Seems clear, doesn't it? Well, now imagine that there is another ship with the same name that sets sail in December. See the problem? Simply using the name isn't enough because it can have more than one meaning.

Namespaces are a URI that is used to get around this type of problem by providing what in law would be called a "meeting of the minds." It is a way to ensure that when the elements and attribute have the same names, the correct meaning is used. This is a good way to avoid conflict. The only alternative would be to guess, which was done in the previous example from the mid-1800s. In case you were wondering, they guessed wrong.

6.5.2. DTD

A Document Type Definition is used to describe and validate an XML document. Essentially, you spell out exactly what to expect in a particular XML document, to avoid confusion. Consider the XML document shown in Listing 6-9, basically a short list of monsters and where they've appeared.

Listing 6-9. An Example XML Document

<?xml version="1.0" encoding="UTF-8"?>
<monsters >
      <monster name="Dracula" books="yes" plays="yes" movies="yes"/>
      <monster name="Alien" books="yes" plays="no" movies="yes"/>
      <monster name="The Thing" books="yes" plays="no" movies="yes"/>
      <monster name="Sweeny Todd" books="yes" plays="yes" movies="no"/>
</monsters>

If confusion concerning names were a possibility, a DTD like the one in Listing 6-10 would then be used.

Listing 6-10. The DTD for Listing 6-9

<!ELEMENT monster EMPTY>
<!ATTLIST monster
      name CDATA #REQUIRED
      books CDATA #REQUIRED
      plays CDATA #REQUIRED
      movies CDATA #REQUIRED
>
<!ELEMENT monsters (monster+)>

All that then would be left to do would be to save it in a folder called namespace on the C: drive and assign the DTD by inserting the following before the first element:

<!DOCTYPE monsters SYSTEM "C:\namespace\sample.dtd">

Just in case you haven't noticed something strange about Document Type Definitions, I want to point out that they are not XML. However, there is an XML equivalent to Document Type Definitions called schemas.

6.5.3. Schema

Schemas have the advantages of being XML and being able to provide greater validation than DTDs. The reason for this is that a schema can describe complex data types beyond the basic dateTime, decimal, integer, and string available with DTDs. This essentially means that it is possible to describe complex types, as shown in Listing 6-11.

Listing 6-11. Schema for Listing 6-9

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--W3C Schema generated by XMLSpy v2006 sp2 U (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
      <xs:complexType name="monsterType">
            <xs:attribute name="name" use="required">
                  <xs:simpleType>
                        <xs:restriction base="xs:string">
                              <xs:enumeration value="Alien"/>
                              <xs:enumeration value="Dracula"/>
                              <xs:enumeration value="Sweeny Todd"/>
                        </xs:restriction>
                  </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="books" use="required">
                  <xs:simpleType>
                        <xs:restriction base="xs:string">
                              <xs:enumeration value="no"/>
                              <xs:enumeration value="yes"/>
                        </xs:restriction>
                  </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="plays" use="required">
                  <xs:simpleType>
                        <xs:restriction base="xs:string">
                              <xs:enumeration value="no"/>
                              <xs:enumeration value="yes"/>
                        </xs:restriction>
                  </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="movies" use="required">
                  <xs:simpleType>
                        <xs:restriction base="xs:string">
                              <xs:enumeration value="no"/>
                              <xs:enumeration value="yes"/>
                        </xs:restriction>
                  </xs:simpleType>
            </xs:attribute>
      </xs:complexType>
      <xs:element name="monsters">
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="monster" type="monsterType"
maxOccurs="unbounded"/>
                  </xs:sequence>
            </xs:complexType>
      </xs:element>
</xs:schema>

Yes, it is longer, but it also better describes the XML document in greater detail than the DTD ever could. This leaves only the "how to assign it?" question, which Listing 6-12 answers.

Listing 6-12. The Document with the Schema Applied

<?xml version="1.0" encoding="UTF-8"?>
<monsters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\namespace\sample.xsd">
      <monster name="Dracula" books="yes" plays="yes" movies="yes"/>
      <monster name="Alien" books="yes" plays="no" movies="yes"/>
      <monster name="Sweeny Todd" books="yes" plays="yes" movies="no"/>
</monsters>


Previous Page
Next Page




JavaScript Editor Free JavaScript Editor     JavaScript Debugger


©