1

Is there any difference between an empty simple element and an empty complex element in a xml file? How can I distinguish them?

On w3schools I found:

  1. empty simple element <element></element> or <element />; in addition it says that "Empty elements can have attributes.".
  2. empty complex element <product pid="1345"/>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Gennaro Arguzzi
  • 769
  • 1
  • 14
  • 27

2 Answers2

2

Being simple or complex is a property of a type, not (directly) of an element. We can say that an element conforms to a type (specifically, a type defined in an XSD Schema), and we can say that the type is simple or complex, but the categories of simple and complex types are overlapping in the kinds of elements that they allow. Specifically, both complex types and simple types might allow empty elements such as <e/>, and elements with text content such as <e>foo</e>. If we see an element that has attributes, or child elements, then we know that it can't conform to any simple type, but if it has neither, then we don't know without looking at the schema.

The answer to your question "how can I distinguish them" is that you need to look in the schema; you can't distinguish them just from the instance alone.

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

An element being simple implies that it has no child elements and no attributes. If an element has child elements or attributes, it is considered to be complex.

Separately, to say that an element is empty is to say that it has no content – not only does it have no child elements, it also has no child text nodes.

Therefore, both simple and complex elements may be empty or non-empty.

Side note: Whether a start tag is self closing, <e/> vs <e></e>, regardless of its element being simple or complex, is insignificant and generally not able to be detected or defined at the XML level.

Examples

  1. <e></e> is simple and empty.
  2. <e>abc</e> is simple and non-empty.
  3. <e a="1"></e> is complex and empty.
  4. <e a="1">abc</e> is complex and non-empty.

See also

Summary

Generally, think of a complex type as having XML substructure and a simple type as lacking XML substructure. Think of empty as implying no content.

kjhughes
  • 106,133
  • 27
  • 181
  • 240