# The XML C parser and toolkit of Gnome ## Namespaces

Main Menu
Home Reference Manual Introduction FAQ Developer Menu Reporting bugs and getting help How to help Downloads Releases XML XSLT Validation & DTDs Encodings support Catalog support Namespaces Contributions Code Examples API Menu XML Guidelines Recent Changes
Related links
Mail archive XSLT libxslt DOM gdome2 XML-DSig xmlsec FTP Windows binaries Solaris binaries MacOsX binaries C++ bindings PHP bindings Pascal bindings Ruby bindings Tcl bindings Bug Tracker
The libxml2 library implements XML namespacessupportbyrecognizing namespace constructs in the input, and does namespacelookupautomatically when building the DOM tree. A namespace declarationisassociated with an in-memory structure and all elements or attributeswithinthat namespace point to it. Hence testing the namespace is a simple andfastequality operation at the user level. I suggest that people using libxml2 use a namespace, and declare it intheroot element of their document as the default namespace. Then they don'tneedto use the prefix in the content but we will have a basis for futuresemanticrefinement and merging of data from different sources. This doesn'tincreasethe size of the XML output significantly, but significantly increasesitsvalue 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 havetopoint to any existing resource on the Web. It will bind all the elementandattributes with that URL. I suggest to use an URL within a domainyoucontrol, and that the URL should contain some kind of version informationifpossible. For example, `"http://www.gnome.org/gnumeric/1.0/"`isagood namespace scheme. Then when you load a file, make sure that a namespace carryingtheversion-independent prefix is installed on the root element of yourdocument,and if the version information don't match something you know, warnthe userand be liberal in what you accept as the input. Also do *not* try tobasenamespace checking on the prefix value. <foo:text> may be exactlythesame as <bar:text> in another document. What really matters is theURIassociated with the element or the attribute, not the prefix string (whichisjust a shortcut for the full URI). In libxml, element and attributes havean`ns`field pointing to an xmlNs structure detailing thenamespaceprefix and its URI. @@Interfaces@@
    xmlNodePtr node;
    if(!strncmp(node->name,"mytag",5)
      && node->ns
      && !strcmp(node->ns->href,"http://www.mysite.com/myns/1.0")) {
      ...
    }
Usually people object to using namespaces together with validitychecking.I will try to make sure that using namespaces won't break validitychecking,so even if you plan to use or currently are using validation Istronglysuggest adding namespaces to your document. A default namespacescheme`xmlns="http://...."`should not break validity even onlessflexible parsers. Using namespaces to mix and differentiate contentcomingfrom multiple DTDs will certainly break current validation schemes. Tochecksuch documents one needs to use schema-validation, which is supportedinlibxml2 as well. See relagx-ngand w3c-schema. Daniel Veillard