I'm aware of the default namespace in XML, e.g. declare xmlns="xxx" for an element, and this namespace will apply to all children without a prefix. My question is, if instead of xmlns="...", xmlns:pre="..." is declared in the element, are unprefixed child elements still associated with the namespace of the parent?
Here is an example:
<?xml version="1.0" ?>
<srv:tutorial xmlns:srv='http://www.w3resource.com/server-side-tutorial'>
<name>php</name>
<name>asp</name>
<name>jsp</name>
</srv:tutorial>
What is the namespace of the three inner name elements?
When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
The xmlns attribute specifies the xml namespace for a document. Note: The xmlns attribute is required in XHTML, invalid in HTML 4.01, and optional in HTML5.
When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.
One of the primary motivations for defining an XML namespace is to avoid naming conflicts when using and re-using multiple vocabularies. XML Schema is used to create a vocabulary for an XML instance, and uses namespaces heavily.
All the three inner 'name' elements belong to the 'default' or 'empty' namespace, not the namespace prefixed with 'srv'.
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