Is it possible to have multiple namespace prefixes in XML?

前端 未结 1 1297
栀梦
栀梦 2021-01-20 19:57

I would like to do something like this:



        
相关标签:
1条回答
  • 2021-01-20 20:09

    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?
    0 讨论(0)
提交回复
热议问题