relopier
relopier

Reputation: 23

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.

Upvotes: 2

Views: 796

Answers (3)

kjhughes
kjhughes

Reputation: 111581

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.

Upvotes: 3

Michael Kay
Michael Kay

Reputation: 163322

(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")

Upvotes: 2

har07
har07

Reputation: 89285

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.

Upvotes: 1

Related Questions