How is targetNamespace used in XML? An explanation with some real examples would be great.
Strictly speaking, targetNamespace doesn't belong to XML per se, but to XML Schema (which is, of course, itself XML). When you define a schema, you define a structure of XML nodes (mainly elements and attributes) that an XML instance (i.e. your "XML file") must conform to.
Usually (not necessarily), those XML nodes will reside in a namespace. This means that when the XML instance is validated against the schema, the nodes in the instance will have to reside in that namespace for the instance to be valid.
The schema's targetNamespace attribute simply defines which namespace that is.
If you want a general overview of namespaces in XML, James Clark's explanation at http://www.jclark.com/xml/xmlns.htm is excellent.
"targetNamespace" is an attribute of an XML Schema Document. When you define an XSD schema, you have to write it in a modular way, with one or more XML Schema Documents for each namespace that you are defining. (Usually you will only be defining one namespace, but you may import schema documents for other namespaces if these are used in your vocabulary.) The targetNamespace of a schema document defines which namespace the element declarations (and other declarations and definitions) in that schema document belong to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With