11/26/2010

What is YAML? What are its advantages over XML? Give examples on how an XML document can be represented by a YAML document.


What is YAML? What are its advantages over XML? Give examples on how an XML document can be represented by a YAML document.

YAML, “YAML Ain’t Markup Language”, is a data serialization language designed to be human-friendly and work well with modern programming languages for common everyday tasks. This specification is both an introduction to the YAML language and the concepts supporting it, and also a complete specification of the information needed to develop applications for processing YAML, from Oren, Clark and Ingy. They also mentions that, “YAML’s indentation-based scoping is similar to Python’s (without the ambiguities caused by tabs). Indented blocks facilitate easy inspection of the data’s structure. YAML’s literal style leverages this by enabling formatted text to be cleanly mixed within an indented structure without troublesome escaping. YAML also allows the use of traditional indicator-based scoping similar to JSON’s and Perl’s. Such flow content can be freely nested inside indented blocks.”

According to Oren, Clark and Ingy, the design goals for YAML are, in decreasing priority,
“Easily readable by humans”, unlike XML, the formatting of YAML does not require a lot of “<xxx>”, which is very difficult to read. It just use simple use “-, #, :, ?“ as a paragraph to define the sequence of the sources.

“YAML data is portable between programming languages and matches the native data structures of agile languages”, YAML integrates and builds upon concepts described by C, Java, Perl, Python, Ruby, RFC0822 (MAIL), RFC1866 (HTML), RFC2045 (MIME), RFC2396 (URI), XML, SAX, SOAP, and JSON. It carried much more than XML.

“YAML is easy to implement and use and expressive and extensible”, XML can only implement by XSLT , or some program which specific to designed to read XML. However, YAML can easily to implement and use by the language which mentioned before.
”YAML has a consistent model to support generic tools” but XML does not

All in all, YAML is trying a more agile way than XML, XML to complete the task.

There are at least two issues that arise in representing this chess club data as XML by David. The first, and simpler, issue is exactly what the best XML representation would be in the abstract. Hhe would propose something like the following as a best attempt in XML:
<?xml version="1.0"?>
<club>
  <players>
    <player id="kramnik"
            name="Vladimir Kramnik"
            rating="2700"
            status="GM" />
    <player id="fritz"
            name="Deep Fritz"
            rating="2700"
            status="Computer" />
    <player id="mertz"
            name="David Mertz"
            rating="1400"
            status="Amateur" />
  </players>
  <matches>
    <match>
        <Date>2002-10-04</Date>
        <White refid="fritz" />
        <Black refid="kramnik" />
        <Result>Draw</Result>
    </match>
    <match>
        <Date>2002-10-06</Date>
        <White refid="kramnik" />
        <Black refid="fritz" />
        <Result>White</Result>
    </match>
  </matches>
</club>

The YAML format simply matches the data structures of dynamic languages better. And it looks nicer too. Here's a YAML representation of the same chess club data:
 
---
players:
  Vladimir Kramnik: &kramnik
    rating: 2700
    status: GM
  Deep Fritz: &fritz
    rating: 2700
    status: Computer
  David Mertz: &mertz
    rating: 1400
    status: Amateur
 
matches:
  -
    Date: 2002-10-04
    White: *fritz
    Black: *kramnik
    Result: Draw
  -
    Date: 2002-10-06
    White: *kramnik
    Black: *fritz
    Result: White

There are a number of nice things about this format. The YAML Web site gives exact specifications, but this brief sample gives you a pretty accurate idea of the basic elements. The spec also includes an intuitive means of including (multi-)paragraph strings. YAML is terse, but still readable. Moreover, quoting is minimal, with data types being inferred from patterns (for example, if it looks like a date, it is treated as a timestamp value unless explicitly string quoted). You can use references to any named target. And, significantly, YAML maintains the distinction between ordered and associative collections. As an added bonus, you can very easily edit YAML in a text editor, by David.

Reference:
Oren Ben-Kiki, Clark Evans, Ingy döt Net (2001-2009), "YAML Ain't Markup Language" Retrieved from:  http://www.yaml.org
David Mertz, Alternator, Gnosis Software, Inc., "XML Matters: YAML improves on XML" Retrieved from:   http://www.ibm.com/developerworks/xml/library/x-matters23.html

沒有留言:

張貼留言