Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 2 XML STRUCTURE Defining Namespaces Looking

Filed under: PHP and XML — webmaster @ 19:29

CHAPTER 2 XML STRUCTURE Defining Namespaces Looking at the previous example, you may have already determined that xmlns:cus=”http:// www.example.com/Customer” is a namespace definition. Usually, and I stress usually, this is not the case; namespaces are created using a special prefixed attribute name and a URI, like so: xmlns:prefix=”URI” Based on this definition, prefixrefers to the namespace prefix you want to use throughout your document to associate certain elements and attributes to a namespace name (URI). In this example, the Numberelement within the Customer element becomes cus:Number, and the Number element within the Itemelement becomes item:Number. Now, the XML clearly distinguishes between the meanings of these two elements. You have removed any ambiguity from the document. These new names being used in the elements are called qualified names, also referred to as QNames. They can be broken down into two parts, separated by a colon: the prefix and the local name. When using namespaced elements, the start and end tags now must contain the qualified name. Again, an exception to this exists, which you will come to in the Default Namespace section. The significant portion of the namespace declaration is the URI (the namespace name). Once bound to a node or element, this will never change. The prefix, however, is not guaranteed. By manipulating the tree, such as moving elements around using the DOM, it is possible a namespace collision may occur. This frequently happens when a namespace defined lower in the tree declares a namespace and uses a prefix, which was used in one of its ancestors. By moving some element as a child of this other element, the prefixes would collide because they refer to two different URIs. It is perfectly valid for the prefix to automatically be changed to one that would not conflict. This is covered in more detail in the section Namespace Scope. Elements containing the namespace definition are not part of the namespace unless prefixed. Listing 2-14 shows the Orderelement within a namespace, because it is prefixed with ord, as specified in the namespace definition. The Order element in Listing 2-15 is not in any namespace even though a namespace is being defined. Listing 2-14. Element Order Within the http://www.example.com/Order Namespace Listing 2-15. Element Order Not Within the http://www.example.com/Order Namespace Namespaces are not required for every element and attribute within a document. You need to remember that namespaces remove ambiguity when there are, or there could be, overlapping names. Looking at the example, the only two elements that require namespacing are Nameand Number. It would have been perfectly valid to not put all other elements into namespaces. Namespaces can also apply to attributes as well: The attribute cid, with the cus prefix, falls within the http://www.example.com/Customer namespace.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Cheap Web Hosting