Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use an empty element in XML?

Tags:

xml

Rank amateur here. I get the instructions on HOW to use an empty element in XML. But why would you? What would it be used for, other than (I guess) to add a blank record when the XML is parsed?

Here's information from W3Schools:

"Empty XML Elements An element with no content is said to be empty. In XML, you can indicate an empty element like this: <element></element> or you can use an empty tag, like this (this sort of element syntax is called self-closing): <element /> The two forms above produce identical results in an XML parser."

like image 888
Steve T. Avatar asked Mar 16 '15 16:03

Steve T.


People also ask

What is use of empty element in XML?

Empty XML ElementsAn element with no content is said to be empty. The two forms produce identical results in XML software (Readers, Parsers, Browsers). Empty elements can have attributes.

What is the purpose of empty element?

In this article, we will see the empty tags in HTML & we will also focus on the purpose of using an empty tag. An empty element is a component that doesn't have any embedded elements or text elements. Empty elements do not have successor nodes.

Is empty string valid XML?

It's not valid xml. Valid xml requires an xml declaration with encoding type and a root node. Whether or not Oracle or SQL Server will accept it anyway is something you can check for yourself. MSSQL column is fine with empty string and even a free text which is not in any XML style.

What is null in XML?

Ways to represent a null value In an XML document, the usual way to represent a null value is to leave the element or attribute empty.


2 Answers

I think you are using "empty" elements to mean "elements with no children of any kind." Self-closing elements -- ones which don't have a separate closing tag -- may still have attributes and aren't empty in that sense.

(X)HTML itself defines "empty" elements, such as <br/>. Again, even <br/> may have class and other attributes.

To the question of how they might be used in your own schemas, you can use empty XML elements as "markers" (borrowing from the way that .NET uses the term "marker interface" for an interface with no members). This is useful when you want to add a "boolean" attribute to an element (like HTML allows valueless attributes such as checked). In such cases, using an attribute would force you to add noise of some kind (in the form of a meaningless or empty value).

I use it this way, for example, to mark speeches as prose in Shakespeare plays:

<speech>
    <speaker role="Archidamus">Archidamus</speaker>
    <prose/>
    <line a="If_you">If you shall chance, Camillo, to visit Bohemia, on</line>
    <line a="the_like">the like occasion whereon my services are now on</line>
    <line a="foot_you">foot, you shall see, as I have said, great</line>
    <line a="difference_betwixt">difference betwixt our Bohemia and your Sicilia.</line>
</speech>

That said, it is kind of a kludge to work around what can be seen as a defect of XML, viz, the inability to represent non-text primitives.

like image 58
harpo Avatar answered Nov 15 '22 22:11

harpo


If a schema requires that a tag be present in a certain order, you may need to include that tag even if it has an empty value. For example, the following fragment from a schema indicates you need tag foo before tag bar:

<xs:element name="parent">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="foo" minOccurs="1" maxOccurs="1" />
      <xs:element name="bar" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

so the following fragment would be valid:

<parent>
  <foo />
  <bar>asdf!</bar>
</parent>

But the following would not:

<parent>
  <bar>asdf!</bar>
</parent>

Additionally, you can use an empty tag to represent an empty value (as opposed to a null value). However, if you're trying to represent a null value in XML, you should really use an xsi:nil attribute

like image 43
Dan Field Avatar answered Nov 15 '22 23:11

Dan Field