next up previous contents
Next: Encoding Fieldbus Data in Up: Representing Fieldbus Data and Previous: XML Attributes and XML   Contents

Identifying XML Data by Name Versus Order

XML data is commonly retrieved by addressing an element by its name (tag) and retrieving its value. However, XML also provides addressing by the order of elements, similar to arrays in conventional programming languages. A developer designing XML documents, may therefore choose between these two addressing schemas. Listing 21 shows an example where fieldbus data is represented by XML code in three ways.

Line 1 to 5 depicts the commonly used addressing schema, where data is encapsulated in XML elements that have unique names. Line 7 to 11 is an alternative where the data is modeled similar to an array. Data can be retrieved by the element's index, similar to an array. It is obvious that the order of the elements is important this way. Line 13 to 17 shows a mixture of the two examples above, where elements contain unique attributes which may be used for addressing.

language=XML
\begin{lstlisting}[caption={Named Elements Versus Ordered Elements}
,label=ex_x...
...,2</property>
<property id=''quality''>good</property>
</item>
\end{lstlisting}

A quick look at the listing shows the advantages of the named addressing schema:

Human legibility:
The use of named addressing outlines the meaning of the data, such as <value>12.2</value>, which greatly improves human legibility.
Extensibility:
As already stated above, addressing based on the order of elements may be hard to extend, especially if the order of the elements has to be altered. On the other hand, this addressing schema fits perfectly when embedding arrays in XML documents.
Validation:
Constraining data by XML Schema is difficult if not impossible when using the same element name for multiple data. The second example in Listing 21 shows a case where the value (float data type) and quality (string data type) are both embedded in an element called ``property''. This way, it is hard to write XML Schema code that constrains the value to a ``float'' data type, while the XML Schema code for the first example is very simple: <xsd:element name="value" type="xsd:float"/>

The above examples make it obvious that the named addressing scheme is the superior method for most fieldbus data. The second example in listing 21 is hard to address and to constrain by XML Schema, and it is meaningless to human readers. The third example has no advantages compared to the first but contains redundant information while adding unneeded complexity to the XML code.

One exception are arrays which are embedded in XML documents. Listing 22 shows a possible method for embedding historic fieldbus data in an XML document.

language=XML
\begin{lstlisting}[caption={Named Elements Versus Ordered Elements}
,label=ex_x...
...ue>
<value ts=''2003-05-27 00:16:16''>12.3</value>
</historic>
\end{lstlisting}


next up previous contents
Next: Encoding Fieldbus Data in Up: Representing Fieldbus Data and Previous: XML Attributes and XML   Contents
Hermann Himmelbauer 2006-09-27