Whatamidoing
Whatamidoing

Reputation: 490

in XML what is the difference between an empty element and an omitted element?

I am working with a SOAP service and was wondering whether sending an HTTP request with a self-closing tag or omitted tag would be any different.

Example: Lets say I have a class -

Class MyElement
{
    type propx = "1"
    type propy = null
}

Which xml would be best to create?

xml with element omitted:

<MyElement>
    <propx>1</propx>
</MyElement>

xml with self-closing/empty tag

<MyElement>
    <propx>1</propx>
    <propy/>
</MyElement>

Upvotes: 0

Views: 577

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

The two documents are different at the XML level, but the semantic meaning of the difference, like everything else in XML, is a matter for agreement between sender and recipient. In some cases an empty element and an absent element might be alternative ways of indicating "not applicable", but in other cases they might have different meanings. For example, in XHTML, there's clearly a difference between an empty <hr/> element and an absent hr element.

In a genealogy application you might use

<children>
  <child>John</child>
  <child/>
  <child>Mary</child>
  <child>Elizabeth</child>
</children>

to indicate that the second child is known to exist, but their name is unknown.

Upvotes: 1

Related Questions