[libxml2.wiki] Create Namespaces



commit 15706daaeba5b1fd88bfe14ccf8e62089973d96b
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Sat Feb 12 17:58:47 2022 +0000

    Create Namespaces

 Namespaces.md | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
---
diff --git a/Namespaces.md b/Namespaces.md
new file mode 100644
index 0000000..d4855b7
--- /dev/null
+++ b/Namespaces.md
@@ -0,0 +1,27 @@
+The libxml2 library implements [XML namespaces](http://www.w3.org/TR/REC-xml-names/) support by recognizing 
namespace constructs 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 libxml2 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 attributes 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/`](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@@
+
+```
+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 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. To check such documents one needs 
to use schema-validation, which is supported in libxml2 as well. See [relagx-ng](http://www.relaxng.org/) and 
[w3c-schema](http://www.w3c.org/XML/Schema).
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]