# The XML C library for Gnome ## Namespaces

Main Menu
Home Introduction FAQ Documentation Reporting bugs and getting help How to help Downloads News XML XSLT Python and bindings libxml architecture The tree output The SAX interface Validation & DTDs Memory Management Encodings support I/O Interfaces Catalog support The parser interfaces Entities or no entities Namespaces Upgrading 1.x code Thread safety DOM Principles A real example Contributions flat page, stylesheet
API Indexes
Alphabetic Constructors Functions/Types Modules Symbols
Related links
Mail archive XSLT libxslt DOM gdome2 XML-DSig xmlsec FTP Windows binaries Solaris binaries Bug Tracker
The libxml library implements XML namespaces support by recognizing namespace contructs in the input, and does namespace lookup automatically when building the DOM tree. A namespace declaration is associated with an in-memory structure and all elements or attributes within that namespace point to it. Hence testing the namespace is a simple and fast equality operation at the user level. I suggest that people using libxml use a namespace, and declare it in the root element of their document as the default namespace. Then they don't need to use the prefix in the content but we will have a basis for future semantic refinement and merging of data from different sources. This doesn't increase the size of the XML output significantly, but significantly increases its value in the long-term. Example:
    <mydoc xmlns="http://mydoc.example.org/schemas/">
       <elem1>...</elem1>
       <elem2>...</elem2>
    </mydoc>
The namespace value has to be an absolute URL, but the URL doesn't have to point to any existing resource on the Web. It will bind all the element and atributes with that URL. I suggest to use an URL within a domain you control, and that the URL should contain some kind of version information if possible. For example, `"http://www.gnome.org/gnumeric/1.0/"` is a good namespace scheme. Then when you load a file, make sure that a namespace carrying the version-independent prefix is installed on the root element of your document, and if the version information don't match something you know, warn the user and be liberal in what you accept as the input. Also do *not* try to base namespace checking on the prefix value. <foo:text> may be exactly the same as <bar:text> in another document. What really matters is the URI associated with the element or the attribute, not the prefix string (which is just a shortcut for the full URI). In libxml, element and attributes have an `ns` field pointing to an xmlNs structure detailing the namespace prefix and its URI. @@Interfaces@@ @@Examples@@ Usually people object to using namespaces together with validity checking. I will try to make sure that using namespaces won't break validity checking, so even if you plan to use or currently are using validation I strongly suggest adding namespaces to your document. A default namespace scheme `xmlns="http://...."` should not break validity even on less flexible parsers. Using namespaces to mix and differentiate content coming from multiple DTDs will certainly break current validation schemes. I will try to provide ways to do this, but this may not be portable or standardized. Daniel Veillard