Empty-Element Tags

Empty-Element Tags

Developers trained in database theory often latch onto the empty-element tag (e.g., <para/> ) as a way to indicate a null value, which they rightly consider to be distinct from 0 or the empty string. From their perspective this makes sense. <para>AA</para> is a para element whose value is the string "AA". <para>A</para> is a para element whose value is the string "A". <para></para> is a para element whose value is the empty string. Finally, <para/> is a para element whose value is null.

This is all perfectly sensible , but it does not reflect the way XML parsers actually behave. An XML parser will produce exactly the same data from <para></para> as from <para/> . There is no detectable difference between the two. They both have the same value, and that value is the empty string, not null.

The right way to indicate a null element is to attach an extra attribute to the element. In particular, the W3C XML Schema Language defines an attribute for exactly this purpose, xsi:nil . The customary xsi prefix is mapped to the namespace URL http://www.w3.org/2001/XMLSchema-instance, and as always the prefix can change as long as the URL stays the same. For example, the following para element genuinely has a null value.

 <para xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:nil="true"/> 

A schema-aware parser may actually report the value of this element as being null. However, it's more likely you'll have to explicitly test each empty element for the presence of an xsi:nil attribute. For example, in DOM to convert an Element object known to be empty to a string, you might write code something like this:

 String elementText; String isNil = element.getAttributeNS(  "http://www.w3.org/2001/XMLSchema-instance", nil); if ("true".equals(isNil)) {  elementText == null; } else { // there is no xsi:nil attribute or its value is false   elementText = ""; } 

It is an error to set xsi:nil="true" on a nonempty element. However, the difference between two tags and one is not important. The para element below is also nil.

 <para xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:nil="true"></para> 

Effective XML. 50 Specific Ways to Improve Your XML
Effective XML: 50 Specific Ways to Improve Your XML
ISBN: 0321150406
EAN: 2147483647
Year: 2002
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net