Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have multiple namespace prefixes in XML?

I would like to do something like this:

<root:secondlevel:thirdlevel
    xmlns:secondlevel="http://secondlevel.com"
    xmlns:secondlevel:thirdlevel="http://thirdlevel.com">
</root:secondlevel:thirdlevel>

Is there a way to do those multiple levels root:secondlevel:thirdlevel as valid XML?

like image 564
Don Rhummy Avatar asked Nov 11 '16 18:11

Don Rhummy


People also ask

Can XML have multiple namespaces?

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.

What is namespace prefix in XML?

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".

What is prefix of XML file?

The prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace . It MAY, but need not, be declared, and MUST NOT be bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace.


1 Answers

No, there can be at most one namespace prefix in XML.

The XML Namespace BNF rules for names are based on QName, which allows only a single PrefixedName:

QName          ::= PrefixedName | UnprefixedName
PrefixedName   ::= Prefix ':' LocalPart
UnprefixedName ::= LocalPart
Prefix         ::= NCName
LocalPart      ::= NCName
NCName         ::= Name - (Char* ':' Char*) /* An XML Name, minus the ":" */

Neither Prefix nor LocalPart allow colon (:) characters, so there can be at most one colon (and at most one Prefix) part to a QName.

Side note: multiple colons are syntactically allowed in base level XML:

STag          ::= '<' Name (S Attribute)* S? '>'
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name          ::= NameStartChar (NameChar)*

But the W3C XML Recommendation is clear that colons should not be used except for namespaces purposes:

Note:

The Namespaces in XML Recommendation [XML Names] assigns a meaning to names containing colon characters. Therefore, authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character.

And Namespaces do not allow multiple namespace prefixes as shown above.

See also:

  • What does the XML syntax with a colon mean?
  • Is a colon a legal first character in an XML tag name?
like image 102
kjhughes Avatar answered Sep 23 '22 00:09

kjhughes