The XML Litmus Test Understanding When and Why to Use XML

原创 2004年10月20日 07:24:00


As the popularity of XML has grown, its usage has spread to every nook and cranny of software application development. This is due to the fact that, unlike previous data formats, XML can easily represent both rigid, highly structured data (such as relational data from a database, program configuration files, or spreadsheets) and semi-structured data (such as a Web page or business document). Besides being able to represent both structured and semi-structured data, XML has a number of characteristics that have caused it to be widely adopted as a data representation format. XML is extensible, platform-independent, and supports internationalization by being fully Unicode compliant. The fact that XML is a text-based format means that when the need arises, one can read and edit XML documents using standard text-editing tools. This has led to the widespread adoption of XML as the lingua franca of information interchange.

This popularity has meant that in certain cases XML is chosen as a data representation format when it is not the best tool for the job. This article provides a clear set of guidelines for determining when XML is the right choice as a data representation for a particular situation. Additionally, some examples of appropriate and inappropriate uses of XML are described.

The XML Litmus Test

In my article Understanding XML I described some of the benefits of using XML as a data representation format. Due to widespread support for XML across various software platforms, the primary benefit of using XML is that it is easier to interoperate between applications on different platforms using XML than with most other data representation formats. The second major benefit of using XML is a side effect of its popularity. There are a large number of off-the-shelf tools for dealing with XML including parsers, schema languages, query languages, programming models, editors, and much more. No other data representation format has such a wide array of tools for processing instances of the format on so many platforms.

The aforementioned benefits give us the XML Litmus Test. XML is the appropriate tool for the job if the following criteria are satisfied by choosing XML as the data representation format for a given application:

  1. There is a need to interoperate across multiple software platforms.
  2. One or more of the off-the-shelf tools for dealing with XML can be leveraged when producing or consuming the data.
  3. Parsing performance is not critical.
  4. The content is not primarily binary content, such as a music or image file.
  5. The content does not contain control characters that aren't carriage return, line feed, or tab because they are illegal in XML.

If the expected usage scenario does not satisfy at least four of the above criteria, then it may not make much sense to use XML as the data representation format for the situation in question.

Even when the usage of XML satisfies the criteria, there are still trade-offs that need to be considered. For example, because XML is a text based format and uses redundant tags for labeling content, there are often complaints about the bloat caused by choosing XML as a data representation format. However, in cases where XML brings the benefit of interoperability and a wide array of technologies for describing and processing the data, then this trade off is worth it. This is exactly the trade off in using XML as the basis for network protocols such as XMPP and SOAP.

Using XML as an application configuration file format is another area where a trade-off has to be considered as well. On the surface, XML seems more complex than is needed for most application configuration file needs. In most cases this is true. However, XML brings a number of benefits to the table. These benefits include internationalization, built in mechanisms for creating comments and escaping text, and widely deployed tools for consuming and producing XML. It is often the case that the cost of writing a parser for a custom configuration file format, as well as other tools, outweighs any benefits from the formats simplicity over XML. Considering this trade off, XML is usually, but not always, the right tool for the job when coming up with application configuration files.

The rest of this article applies the XML Litmus Test to a number of established uses of XML.

Some Appropriate Uses of XML

XML was originally intended as a language for defining new document formats for the World Wide Web. XML is derived from the Standard Generalized Markup Language (SGML), and can be considered to be a meta-language, a language for defining markup languages. The benefits of using XML on the Web are self-apparent. On a widespread global network such as the World Wide Web, interoperability across platforms is key, as is the fact that it fully supports internationalization by being Unicode compliant.

One popular usage of XML on the Web today is for XML syndication feeds such as RSS 1.0 or RSS 2.0. Below is a sample RSS 2.0 feed.

<rss version="2.0">
    <title>MSDN XML Developer Center</title>
    <description> Extensible Markup Language (XML) is the universal 
     format for data on the Web. XML allows developers to easily 
     describe and deliver rich, structured data from any application 
  in a standard, consistent way. XML does not replace HTML; rather,
  it is a  complementary format. 
     <title> XML Files: XPath Selections and Custom Functions, 
      and More</title>
     <description> Get your questions about XPath selections, 
 custom functions, and more answered in this month's column.
     <title> Extreme XML: XML Serialization in the .NET Framework </title>
     <description> Dare Obasanjo discusses XML serialization and how 
      you can use it within the .NET Framework to improve 
   interoperability and meet W3C standards.  

Using XML as the data representation format for syndication feeds passes the XML Litmus Test. Interoperability and support for internationalization is a goal of every Web technology, so the benefits of using XML for content syndication is clear in this regard. Secondly, syndication feeds are primarily textual and do not usually contain significant amounts of binary data or control characters. Finally, a number of applications can be built for processing and displaying syndication using off-the-shelf XML technologies. For example, desktop aggregators like RSS Bandit use XSLT for displaying RSS feeds using multiple layouts. Another example is the MSDN Web site, which uses XSLT for rendering RSS feeds to Web browsers in a human readable format as shown by the RSS feed for the MSDN XML Developer Center.

Another area where XML usage is beginning to flourish is as the data representation format for business documents. Not only have office productivity suites such as Microsoft Office begun to use XML as the basis of their document storage formats, but a number of vertical industries have standardized on XML schemas for information interchange. Examples of standardized XML vocabularies for information exchange in specific vertical industries include HR-XML for human resources, HL7-XML for the healthcare industry, XBRL for financial reporting, and much more.

Applying the criteria in the XML Litmus Test to using XML for representing business documents also shows this to be an appropriate usage of XML. First of all, business documents are primarily text so they satisfy the criteria around binary data and illegal XML characters. When exchanging business documents with other entities, ensuring that the documents are platform neutral is an significant factor. This is particularly important because it is unlikely that different business entities run their software on the same platforms or even that different organizations within a particular business entity are running a homogenous network. In typical usage scenarios for business documents, parsing performance is important. After all, no one wants a word processor to take minutes to load a file, and using XML does not require significant enough overhead to make this suboptimal. Finally, the wide array of tools for processing XML comes in very handy for creating and managing business document workflows. Declarative schema languages such as W3C XML Schema and Schematron can be used to enforce constraints on business documents entering or leaving the system. XML Transformation languages such as XSLT can be used to convert between different XML business document formats or publish them as non-XML formats, such as HTML or PDF. Documents can be filtered and routed using XML pattern languages, such as XPath to find documents with characteristics of interest. And the list goes on.

An Inappropriate Use of XML

One usage of XML that I've considered to be inappropriate is the use of XML as a syntax for programming languages, such as is done in o:XML. Consider the following o:XML fragment for a method that takes three string arguments and returns a date element.

<!-- procedure definition -->
<o:procedure name="ex:formatDate">
  <o:param name="day"/>
  <o:param name="month"/>
  <o:param name="year" />
      <day><o:eval select="$day"/></day>
      <month><o:eval select="$month"/></month>
      <year><o:eval select="$year"/></year>

<!-- procedure call -->
<!-- 'year' has a default value and so is optional -->
<ex:formatDate year="2002" month="'Aug'" day="31"/>

Compare that snippet to the following XQuery expression that does the same thing:

declare function ex:formatDate($day as xsd:string, $month as xsd:string, $year as xsd:string) 
   as element(date)

ex:formatDate("31", "Aug", "2002")

The XQuery version is less verbose than the o:XML version, yet performs the same task. The question then is whether the benefits of XML make up for the increased verbosity of the formatDate function in o:XML. Applying the XML Litmus Test, I'd say the answer is no. The ASCII-based text format of a language such as XQuery is just as interoperable as creating an equivalent language with similar identifiers, but using XML instead. Secondly, of the myriad tools for working with XML, the main one that applies in the case of o:XML is the existence of XML parsers that make processing language tokens in o:XML easier than parsing XQuery. However, as someone who's written his share of language parsers, in a previous life I designed and implemented SiXDML, I'd argue that the bulk of the work of creating a compiler has little to do with processing language tokens and more to do with the execution logic. The amount of development time saved is a small part of the overall cost of writing a compiler. However, every user of the language now has to deal with the more complex syntax of the language and increased verbosity if choosing o:XML over XQuery.


XML is a powerful tool in the software developer's toolbox, but like all tools it has tasks it is excels at and others for which it is not as well suited. The XML Litmus Test provides a simple set of guidelines for weighing the costs and benefits of using XML in a software application or architecture.

SQL_CASE WHEN &临时表 &xml to table 混合实例

实现从XML读取条件,并返回过滤结果中的指定部分。from   to  学习列表: case when (在select、where、order by 用法不同,理解并记忆)临时表和表变量...
  • Shiyaru1314
  • Shiyaru1314
  • 2015年07月07日 19:37
  • 710

XML over TCP

当我们在TCP上传输XML消息时,我们会遇到一点麻烦,因为我们很难根据XML协议去判断一条消息是否结束。如果要完整的支持XML over TCP,只有两种办法:1,加一个header去标明数据的长度2...
  • wwwsq
  • wwwsq
  • 2010年02月17日 11:30
  • 3193

#One paper per week# Why We Twitter: Understanding Microblogging Usage and Communities

论文题目:Why We Twitter Understanding Microblogging Usage and Communities 论文地址:
  • John159151
  • John159151
  • 2015年05月18日 15:58
  • 579

Two classes have the same XML type name 排错

:文章来源   今天遇到一个问题,webservice发布的时候报下面的错误:   ...
  • jcy7523
  • jcy7523
  • 2015年08月15日 19:51
  • 1243


问题android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi-v4/
  • Notzuonotdied
  • Notzuonotdied
  • 2017年10月03日 08:50
  • 235


  • wj903829182
  • wj903829182
  • 2017年06月17日 22:14
  • 1372

mybatis **-mapping.xml文件中if test表达式逻辑判断“并且”,不能用&&

用and 例如:
  • StephenLiousYuan
  • StephenLiousYuan
  • 2017年03月13日 14:43
  • 3487


201.Why does performance degrade when many UPDATE, INSERT or DELETE statements are issued  on a tab...
  • rlhua
  • rlhua
  • 2013年11月26日 15:32
  • 10624

OCP 1Z0 052 V9.02 201

178.Which statement is true about the Manageability Monitor (MMON) background process?   A. It tran...
  • jgmydsai
  • jgmydsai
  • 2014年06月27日 21:29
  • 2384

Why should I use XML instead of HTML?

google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";...
  • java169
  • java169
  • 2008年05月21日 02:27
  • 100
您举报文章:The XML Litmus Test Understanding When and Why to Use XML