I have been working with XML for a while now, but some things I have just used without really understanding why they work the way they do. Now I would like to change that.
This question is about namespaces.
This is what I think I have understood so far:
- The namespace can be any string. It is meant to be a unique identifier world wide.
- In order to qualify an XML element with a namespace, you cannot simply prefix the namespace in front of the element name, you need to use a separate prefix which needs to be declared at the beginning of the scope you want to apply the namespace to.
- You are free to choose the prefix.
- You are not free to choose the namespace (?)
- Although a namespace doesn’t necessarily reference anything as a URL, it can very well reference a targetNamespace.
Now, my question, does every namespace reference a targetNamespace ? When I declare xsi to be the prefix for XML Schema, does XML Schema declare that same namespace as its targetNamespace somewhere ? (where?). When I declare xsl to be the prefix for the XSLT namespace, does that mean that the XSLT namespace is declared somewhere as targetNamespace (where?)?
If we can have namespaces without any reference to a targetNamespace, in what situations would we use them? Why would we need them ?
You were doing OK until
Whether or not you are free to choose the namespace depends on what you want to do. If you are making up a new vocabulary you get to choose the namespace (or no namespace). if you want to use an existing vocabulary such as XHTML or MathML or DocBook or … then if course, you have to use the namespace specified.
Terminology problem here I think so I’m not sure what you mean. the namespace spec itself does not have any concept of “targetNamespace”. The term is used of a w3c XML schema to say which namespace the elements described in the schema come from the “target namespace” of the schema. So it isn’t a term you can really apply to a single instance of a namespace in an XML document.
so….
As noted above I’m not sure this question has meaning, but the answer is essentially “no”.
there is a schema for schema that declares the attributes used traditionally for the xsi prefix, and in that sense it will have this namespace as its target namespace, yes.
Any namespace defined without reference to XSD schema doesn’t have anything that could be called a targetNamespace. However if you have an XSD based workflow you can always create such a schema, either from scratch or by converting a DTd or RelaxNg schema. So whether or not there is an XSD schema for a namespace is not an inherent property of the namespace. So for example the XHTML namespace http://www.w3.org/1999/xhtml for a long time (and again, in its html5 incarnation) does not have any official XSD schema associated that has that URI as its target namespace.