XML Element

XML > Structure > XML Element

An XML Element is the basic construct within an XML Document.

An XML Element may contain text, Attributes, other Elements as well as Entity References, CDATA and Processing Instructions, this allows complex hierarchical data to be described.

XML Element Examples

In practice this means an XML element can look like the following examples

Empty Element

<MyElement/>

Element with no content

<MyElement></MyElement>

There is a distinct difference between an empty element and an element with no content, however very few parsers choose to interpret them differently.

Element with text

<MyElement>Some Text</MyElement>

Typically used to hold the value for a property

Element with Attributes

<MyElement myAttribute="attribute value">Some Text</MyElement>

Attributes typically enrich to the data contained within the element, i.e. define the language the text is in, provide unique ID by which the element can be referenced elsewhere within the document etc.
Attributes are typically considered to contain secondary/meta information, although there are no hard and fast rules.

Element with Child Elements

<MyElement>
     <ChildElement/>
</MyElement>

By nesting elements complex hierarchies of data can be constructed. However these hierarchies are tree like. When you start placing real world data in them you quickly hit problems sharing entities between different branches of a tree. These issues can be overcome using unique ID's and references. See Describing Complex Structures in XML.

Element with Mixed Data

<MyElement>Some <b>Mixed</b> Data</MyElement>

Mixed data can contain text and child elements within the containing element. This is typically only used to mark up data (HTML etc).

Its typically only used to hold mark-up/formatted text entered by a person, it is typically not he best choice for storing machine readable data as adds significant complexity to the parser. Where possible it should be avoided.

Namespaces

Namespaces are a concept introduced when an XSD Schema is used, it allows complex XML documents to be broken data down by namespaces.

A namespace is either applied to an element using an aliased prefix

<ns:MyElement>Data</ns:MyElement>

Or via a default namespace declaration

<MyElement xmlns="myNamespace">Data</MyElement>

Namespaces are complex and discussed in more detail under XML Namespaces

Special Characters

If you need to put control characters (<,>,&,",') into an elements text value, this could cause the parser to miss understand the resulting document, in order to prevent this the values need to be escaped, see Escaping XML Data.

EBNF Syntax

The syntax for an element is described by the W3C as using EBNF as follows.

[39]    element        ::=  EmptyElemTag | STag content ETag
[40]    STag           ::=  '<' Name (S Attribute)* S? '>'
[41]    Attribute      ::=  Name Eq AttValue
[42]    ETag           ::=  '</' Name S? '>'
[43]    content        ::=  CharData? ((element | Reference | CDSect | PI | Comment) CharData?)*
[44]    EmptyElemTag   ::=  '<' Name (S Attribute)* S? '/>'

See Also

Try Liquid XML Free and see how we can help you today Free Trial