| Previous | Next
Namespace SyntaxNamespaces disambiguate elements with the same name from each other by assigning elements and attributes to URIs. Generally, all the elements from one XML application are assigned to one URI, and all the elements from a different XML application are assigned to a different URI. These URIs are sometimes called namespace names. The URIs partition the elements and attributes into disjoint sets. Elements with the same name but different URIs are different elements. Elements with the same name and the same URIs are the same. Most of the time there's a one-to-one mapping between namespaces and XML applications, though a few applications use multiple namespaces to subdivide different parts of the application. For instance, XSL uses different namespaces for XSL Transformations (XSLT) and XSL Formatting Objects (XSL-FO). Qualified Names, Prefixes, and Local PartsSince URIs frequently contain characters such as /, %, and ~ that are not legal in XML names, short prefixes such as rdf:description xlink:type xsl:template Everything before the colon is called the prefix. Everything after the colon is called the local part. The complete name including the colon is called the qualified name, QName, or raw name. The prefix identifies the namespace to which the element or attribute belongs. The local part identifies the particular element or attribute within the namespace. In a document that contains both SVG and MathML Prefixes may be composed from any legal XML name character except the colon. Prefixes beginning with the three letters Binding Prefixes to URIsEach prefix in a qualified name must be associated with a URI. For example, all XSLT elements are associated with the http://www.w3.org/1999/XSL/Transform URI. The customary prefix TIP: You can't use the URI in the name directly. For one thing, the slashes in most URIs aren't legal characters in XML names. However, it's occasionally useful to refer to the full name without assuming a particular prefix. One convention used on many XML mailing lists and in XML documentation is to enclose the URI in curly braces and prefix it to the name. For example, the qualified name Prefixes are bound to namespace URIs by attaching an <rdf:RDF xmlns:rdf="http://www.w3.org/TR/REC-rdf-syntax#"> <rdf:Description about="http://www.cafeconleche.org/examples/impressionists.xml"> <title> Impressionist Paintings </title> <creator> Elliotte Rusty Harold </creator> <description> A list of famous impressionist paintings organized by painter and date </description> <date>2000-08-22</date> </rdf:Description> </rdf:RDF> Bindings have scope within the element where they're declared and within its contents. The The prefix can be declared in the topmost element that uses the prefix or in any ancestor thereof. This may be the root element of the document, or it may be an element at a lower level. For instance, the Dublin Core elements could be attached to the http://purl.org/dc/ namespace by adding an Example 4-3. A document containing both SVG and XLinks<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> <catalog> <rdf:RDF xmlns:rdf="http://www.w3.org/TR/REC-rdf-syntax#"> <rdf:Description xmlns:dc="http://purl.org/dc/" about="http://www.cafeconleche.org/examples/impressionists.xml"> <dc:title> Impressionist Paintings </dc:title> <dc:creator> Elliotte Rusty Harold </dc:creator> <dc:description> A list of famous impressionist paintings organized by painter and date </dc:description> <dc:date>2000-08-22</dc:date> </rdf:Description> </rdf:RDF> <painting> <title>Memory of the Garden at Etten</title> <artist>Vincent Van Gogh</artist> <date>November, 1888</date> <description> Two women look to the left. A third works in her garden. </description> </painting> <painting> <title>The Swing</title> <artist>Pierre-Auguste Renoir</artist> <date>1876</date> <description> A young girl on a swing. Two men and a toddler watch. </description> </painting> <!-- Many more paintings... --> </catalog> A DTD for this document can include different content specifications for the In this example, the elements without prefixes, such as It is possible to redefine a prefix within a document so that in one element the prefix refers to one namespace URI, while in another element it refers to a different namespace URI. In this case, the closest ancestor element that declares the prefix takes precedence. However, in most cases redefining prefixes is a very bad idea that only leads to confusion and is not something you should actually do. Namespace URIsMany XML applications have customary prefixes. For example, SVG elements often use the prefix Namespace URIs do not necessarily point to any actual document or page. In fact, they don't have to use the http scheme. They might even use some other protocol like mailto in which URIs don't even point to documents. However, if you're defining your own namespace using an http URI, it would not be a bad idea to place some documentation for the specification at the namespace URI. The W3C got tired of receiving broken-link reports for the namespace URIs in their specifications, so they added some simple pages at their namespace URIs. For more formal purposes that offer some hope of automated resolution and other features, you can place a Resource Directory Description Language (RDDL) document at the namespace URI. This possibility will be discussed further in . You are by no means required to do this, though. Many namespace URIs lead to 404-Not Found errors when you actually plug them into a web browser. Namespace URIs are purely formal identifiers. They are not the addresses of a page, and they are not meant to be followed as links. Parsers compare namespace URIs on a character-by-character basis. If the URIs differ in even a single normally insignificant place, then they define separate namespaces. For instance, http://www.w3.org/1999/02/22-rdf-syntax-ns#, http://WWW.W3.ORG/1999/02/22-rdf-syntax-ns#, http://www.w3.org/1999/02/22-rdf-syntax-ns/, and http://www.w3.org/1999/02/22-rdf-syntax-ns/index.rdf all point to the same page. However, only the first is the correct namespace name for the RDF. These four URLs identify four separate namespaces. Setting a Default Namespace with the xmlns AttributeYou often know that all the content of a particular element will come from a particular XML application. For instance, inside an SVG <svg xmlns="http://www.w3.org/2000/svg" > <ellipse rx="110" ry="130" /> <rect x="4cm" y="1cm" /> </svg> Here, although no elements have any prefixes, the The attributes are a different story. Default namespaces only apply to elements, not to attributes. Thus in the previous example the You can change the default namespace within a particular element by adding an Example 4-4. An XML document that uses default namespaces<?xml version="1.0"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink"> <head><title>Three Namespaces</title></head> <body> <h1 >An Ellipse and a Rectangle</h1> <svg xmlns="http://www.w3.org/2000/svg" > <ellipse rx="110" ry="130" /> <rect x="4cm" y="1cm" /> </svg> <p xlink:type="simple" xlink:href="ellipses.html"> More about ellipses </p> <p xlink:type="simple" xlink:href="rectangles.html"> More about rectangles </p> <hr/> <p>Last Modified May 13, 2000</p> </body> </html> The default namespace does not apply to any elements or attributes with prefixes. These still belong to whatever namespace to which their prefix is bound. However, an unprefixed child element of a prefixed element still belongs to the default namespace. Attribute Declarations for xmlnsWhen namespaces are only being used to identify the elements and attributes from a particular XML application, and not to distinguish different elements with the same name, a DTD can attach a fixed <!ATTLIST svg xmlns CDATA #FIXED "http://www.w3.org/2000/"> This allows you to omit A document does not need to be valid to take advantage of this. All that's required is that the parser read the DTD. All parsers will read the internal DTD subset and process any such |