2

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
relopier
  • 23
  • 3

3 Answers3

3

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.
Progman
  • 16,827
  • 6
  • 33
  • 48
kjhughes
  • 106,133
  • 27
  • 181
  • 240
2

(a) Yes, your example is well-formed.

(b) The h element is in no namespace. An unprefixed element is in a namespace only if it is within the scope of a default namespace declaration (xmlns="some-url")

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

Your example XML is valid so, yes, h element can be withou prefix s:, and it means that h is in empty namespace, not in the same namespace as referenced by the prefix s:. Descendant elements without prefix inherit default namespace implicitly, but not with prefixed namespaces.

har07
  • 88,338
  • 12
  • 84
  • 137