Search code examples
xmlxml-namespacesxml-validation

With a non-default namespace in XML should every tag have a prefix?


I'm a beginner in XML. I just studied namespaces, and I struggle a little bit so my question is in this example,

<s:root xmlns:s="help.com">
  <h></h>
</s:root>

can we have <h> without the s: prefix ?

If yes, is <h> included in the s: namespace or not?

I know is a dumb questions, but every one start dumb. Thanks for your answers.


Solution

  • Your question is not "dumb" at all — actually reflects advanced emerging understanding.

    "Should" XML

    "Should" XML questions may be answered at two levels: well-formedness and validity.

    1. Your XML is indeed well-formed. It follows all of the rules for being XML. It even follows all of the rules for being namespace-well-formed. Under the rules of XML well-formedness, yes, you can have an h element without a s: prefix. (Under the rules of namespace-well-formedness, you can as well — you just couldn't have a d:h element with an undeclared d namespace prefix.)

    2. Your XML may or may not be valid. In XML terms, to be valid implies that it follows the rules given by a schema (commonly XSD; less commonly DTD, Relax NG, Schematron, ...). Under the rules of validity given by an XML schema, you may or may not be able to have an h element in that position — we would have to have an XML schema to know.

    XML Namespaces

    For your XML,

    <s:root xmlns:s="help.com">
      <h></h>
    </s:root>
    
    • root is in the help.com namespace.
    • h is in no namespace.

    For this XML,

    <root xmlns="help.com">
      <h></h>
    </root>
    
    • root is in the help.com namespace.
    • h is also in the help.com namespace because xmlns="help.com" declares a default namespace that applies to root and all descendent elements lacking namespace declarations.