Jump to content

XML: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Shlomital (talk | contribs)
m XSL-FO is not a replacement for CSS
Ajcumming (talk | contribs)
Line 192: Line 192:
**[http://www.relaxng.org Relax NG homepage]
**[http://www.relaxng.org Relax NG homepage]
* '''Tutorials'''
* '''Tutorials'''
**[http://sqlzoo.net/xml/ A Gentle Introduction to XML]
**[http://xmlzoo.net/ A Gentle Introduction to XML] from XMLzoo
**http://www.w3schools.com/xml/default.asp
**http://www.w3schools.com/xml/default.asp
**http://www.w3schools.com/xml/xml_examples.asp
**http://www.w3schools.com/xml/xml_examples.asp

Revision as of 21:14, 19 March 2005


The Extensible Markup Language (XML) is a W3C recommendation for creating special-purpose markup languages. It is a simplified subset of SGML, capable of describing many different kinds of data. Its primary purpose is to facilitate the sharing of structured text and information across the Internet. Languages based on XML (for example, RDF, RSS, MathML, XSIL and SVG) are themselves described in a formal way, allowing programs to modify and validate documents in these languages without prior knowledge of their form.

History

XML was developed by Tim Bray after he worked on a online dictionary project that was sponsored by IBM, Oxford University Press and the University of Waterloo. Due to the large amounts of data that had to be stored and processed, commercial software engineers were brought in to find a solution to the problem of indexing and storing that data. In an interview for the Association for Computing Machinery Bray said that he was brought into the project and was shown the initial structure that had been developed for the dictionary: "little embedded tags saying entry, word, and then pronunciation, etymology, a brief quotation, and the date, source, text, and so on." (ACM Queue, 2005) This became the precursor to XML. After this technology was developed for the dictionary project, Bray formed the Open Text Corporation, developed a search engine and was invited by the W3C to be an editor on their XML specification.

Strengths and weaknesses

The features of XML that make it particularly appropriate for data transfer are:

XML is also heavily used as the format for document storage and processing, both online and offline, and offers several benefits:

  • robust, logically-verifiable format based on international standards
  • hierarchical structure suitable for most (but not all) types of documents
  • plain text files, unencumbered by licenses or restrictions
  • platform-independent, and so relatively immune to changes in technology
  • has already been in use (as SGML) for long over a decade, and is very popular by itself, so there is extensive experience and software available.

For certain applications, the format also has the following weaknesses:

  • XML syntax is fairly verbose and partially redundant. This can hurt human readability and application efficiency, and yields higher storage costs. It can also make XML difficult to apply in cases where bandwidth is limited, though compression can reduce the problem in some cases. This is particularly true for multimedia applications running on cellphones, PDAs which want to use XML to describe images and video.
  • XML syntax contains a number of obscure features due to its legacy of SGML compatibility.
  • XML still often requires further parsing to extract individual values.
  • No facilities exist for randomly accessing or updating only portions of a document.
  • Modelling overlapping (non-hierarchical) data structures requires extra effort.
  • Mapping XML to the relational or object oriented paradigms is often cumbersome.

Correctness in an XML document

For an XML document to be correct, it must be:

  • Well-formed. A well-formed document conforms to all of XML's syntax rules. For example, if a non-empty element has an opening tag with no closing tag, it is not well-formed. A document that is not well-formed is not considered to be XML; a parser is required to refuse to process it.
  • Valid. A valid document has data that conforms to a particular set of user-defined content rules that describe correct data values and locations. For example, if an element in a document is required to contain text that can be interpreted as being an integer numeric value, and it instead has the text "hello", is empty, or has other elements in its content, then the document is not valid.

Well-formed documents

An XML document is text, usually a particular encoding of Unicode such as UTF-8 or UTF-16, although other encodings may be used.

A well-formed document must conform to the following rules, among others:

  • One and only one root element exists for the document. Note that the prolog, xml declaration, processing instructions or comments can exist parallel to the root element.
  • Non-empty elements are delimited by both a start-tag and an end-tag.
  • Empty elements may be marked with an empty-element (self-closing) tag, such as <IAmEmpty/>. This is equal to <IAmEmpty></IAmEmpty>.
  • All attribute values are quoted, either single (') or double (") quotes. Single quotes close a single quote and double quotes close a double quote, with strict nesting enforced.
  • Tags may be nested but may not overlap. Each non-root element must be completely contained in another element.
  • The document complies to its character set definition. The charset is usually defined in the xml declaration but it can be provided by the transport protocol, such as HTTP. If no charset is defined, usage of a Unicode encoding is assumed, defined by the Unicode byte order mark. If the mark does not exist, UTF-8 is the default.

Element names are case-sensitive. For example, the following is a well-formed matching pair

<MySample> ... </MySample>

whereas this is not

<MySample> ... </Mysample>

The careful choice of names for XML elements will convey the meaning of the data in the markup. This increases human readability while retaining the rigor needed for software parsing.

For example, a simple recipe might be expressed in XML as:

       <?xml version="1.0" encoding="UTF-8"?>
       <Recipe name="bread" prep_time="5 mins" cook_time="3 hours">
          <title>Basic bread</title>
          <ingredient amount="3" unit="cups">Flour</ingredient>
          <ingredient amount="0.25" unit="ounce">Yeast</ingredient>
          <ingredient amount="1.5" unit="cups">Warm Water</ingredient>
          <ingredient amount="1" unit="teaspoon">Salt</ingredient>
          <Instructions>
             <step>Mix all ingredients together, and knead thoroughly.</step>
             <step>Cover with a cloth, and leave for one hour in warm room.</step>
             <step>Knead again, place in a tin, and then bake in the oven.</step>
          </Instructions>
       </Recipe>

Choosing meaningful names implies the semantics of elements and attributes to a human reader without reference to external documentation. However, this can lead to verbosity, which complicates authoring and increases file size.

Valid documents

An XML document that complies with a particular schema, in addition to being well-formed, is said to be valid.

An XML schema is a description of a type of XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic constraints imposed by XML itself. A number of standard and proprietary XML schema languages have emerged for the purpose of formally expressing such schemas, and some of these languages are XML-based, themselves.

Before the advent of generalised data description languages such as SGML and XML, software designers had to define special file formats or small languages to share data between programs. This required writing detailed specifications and special-purpose parsers and writers.

XML's regular structure and strict parsing rules allow software designers to leave parsing to standard tools, and since XML provides a general, data model-oriented framework for the development of application-specific languages, software designers need only concentrate on the development of rules for their data, at relatively high levels of abstraction.

Well-tested tools exist to validate an XML document "against" a schema: the tool automatically verifies whether the document conforms to constraints expressed in the schema. Some of these validation tools are included in XML parsers, and some are packaged separately.

Other usages of schemas exist: XML editors, for instance, can use schemas to support the editing process.

DTD

The oldest schema format for XML is the Document Type Definition (DTD), inherited from SGML. While DTD support is ubiquitous due to its inclusion in the XML 1.0 standard, it is seen as limited for the following reasons:

  • It has no support for newer features of XML, most importantly namespaces.
  • It lacks expressivity. Certain formal aspects of an XML document cannot be captured in a DTD.
  • It uses a custom non-XML syntax, inherited from SGML, to describe the schema.

XML Schema

A newer XML schema language, described by the W3C as the successor of DTDs, is W3C XML Schema (WXS), also called just XML Schema or more informally referred to in terms of the initialism for XML Schema instances, XSD (XML Schema Definition). XSDs are far more powerful than DTDs in describing XML languages. They use a rich datatyping system, allow for more detailed constraints on an XML document's logical structure, and are required to be processed in a more robust validation framework. Additionally, XSDs use an XML based format, which makes it possible to use ordinary XML tools to help process them, although WXS implementations require much more than just the ability to read XML. Criticisms of WXS include the following:

  • The specification is very large, which makes it difficult to understand and implement.
  • The XML-based syntax leads to verbosity in schema description, which makes XSDs harder to read and write.

RELAX NG

Another XML popular schema language is RELAX NG. Initially standardized by OASIS, RELAX NG is now also an ISO international standard (as part of DSDL). It has two formats: an XML based syntax and a non-XML compact syntax. The compact syntax aims to increase readability and writability, but since there is a well-defined way to translate compact syntax to the XML syntax and back again, the advantage of using standard XML tools is not lost. Compared to XML Schema, RELAX NG has a simpler definition and validation framework, making it easier to use and implement. It also has the ability to use any datatype framework on a plug-in basis; for example, a RELAX NG schema author can require values in an XML document to conform to definitions in XML Schema Datatypes.

Other schema languages

Some schema languages not only describe the structure of a particular XML format but also offer limited facilities to influence processing of individual XML files that conform to this format. DTDs and XSDs both have this ability; they can for instance provide attribute defaults. RELAX NG intentionally does not provide these facilities.

Displaying XML on the web

Extensible Stylesheet Language (XSL) is a supporting technology that describes how to format or transform the data in an XML document. The document is changed to a format suitable for browser display. The process is similar to applying a CSS to an HTML document for rendering. A line at the top of an XML document:

<?xml-stylesheet type="text/xsl" href="transform.xsl"?>

declares that transform.xsl is an XSLT style sheet holding instructions to transform the XML into HTML.

An XML document may also be rendered directly in some browsers such as e.g. Internet Explorer 5 or Mozilla with the stylesheet language CSS. This process is still not yet stable as of March 2004 in those browsers; in other browsers, such as Opera, this works very well. In order to allow CSS styling, the XML document must include a special reference to a style sheet:

<?xml-stylesheet type="text/css" href="myStyleSheet.css"?>

This is different from the standard HTML way to apply a stylesheet, which uses the <link> element.

While browser-based XML rendering develops, the alternative is conversion into HTML or PDF or other formats on the server. Programs like Cocoon process an XML file against a stylesheet (and can perform other processing as well) and send the output back to the user's browser without the user needing to be aware of what has been going on in the background.

XML extensions

  • XPath It is possible to refer to individual components of an XML document using XPath. This allows stylesheets in (for example) XSL and XSLT to dynamically "cherry-pick" pieces of a document in any sequence needed in order to compose the required output.
  • XQuery is to XML what SQL is to relational databases.
  • XML namespaces enable the same document to contain XML elements and attributes taken from different vocabularies, without any naming collisions occurring.
  • XML Signature defines the syntax and processing rules for creating digital signatures on XML content.
  • XML Encryption defines the syntax and processing rules for encrypting XML content.

Processing XML files

SAX and DOM are APIs widely used to process XML data. SAX is used for serial processing whereas DOM is used for random-access processing. Another form of XML Processing API is data binding, where XML data is made available as a strongly typed programming language data structure, in constrast to the DOM. Example data binding systems are the Java Architecture for XML Binding (JAXB) [1] and the Strathclyde Novel Architecture for Querying XML (SNAQue) [2].

A filter in the Extensible Stylesheet Language (XSL) family can transform an XML file for displaying or printing.

  • XSL-FO transforms XML files for display. XSL-FO is basically an XML-based page layout language.
  • XSLT transforms XML to other formats, such as HTML, other vocabularies of XML, and any other plain-text format.
  • XQuery[3] is a W3C language for querying, constructing and transforming XML data.
  • XPath[4] is a path expression language for selecting data within an XML file. XPath is a sublanguage of both XQuery and XSLT.

The native file format of OpenOffice.org and AbiWord is XML. Some parts of Microsoft Office 11 will also be able to edit XML files with a user-supplied schema (but not a DTD). There are dozens of other XML editors available.

Versions of XML

There are two current versions of XML. The first, XML 1.0, was initially defined in 1998. It has undergone minor revisions since then, without being given a new version number, and is currently in its third edition, as published on February 4, 2004. It is widely implemented and still recommended for general use. The second, XML 1.1, was initially published on the same day as XML 1.0 Third Edition. It contains features — some contentious — that are intended to make XML easier to use for certain classes of users (mainframe programmers, mainly). XML 1.1 is not very widely implemented and is recommended for use only by those who need its unique features.

XML 1.0 and XML 1.1 differ in the requirements of characters used for element names, attribute names etc.: XML 1.0 only allows characters which are defined in Unicode 2.0, which includes most world scripts, but excludes scripts which only entered in a later Unicode version, such as Mongolian, Cambodian, Amharic, Burmese, etc.. XML 1.1 only disallows certain control characters, which means that any other character can be used, even if it is not defined in the current version of Unicode.

It should be noted here that the restriction present in XML 1.0 only applies to element/attribute names: both XML 1.0 and XML 1.1 allow for the use of full Unicode in the content itself. Thus XML 1.1 is only needed if in addition to using a script added after Unicode 2.0 you also wish to write element and attribute names in that script.

Other minor changes between XML 1.0 and XML 1.1 are that control characters are now allowed to be included but only when escaped, and two special Unicode line break characters are included, which must be treated as whitespace.

XML 1.0 documents are well-formed XML 1.1 documents with one exception: XML documents that contain unescaped C1 control characters are now malformed: this is because XML 1.1 requires the C1 control characters to be escaped with numeric character references.

There are also discussions on an XML 2.0, although it remains to be seen if such will ever come about. XML-SW (SW for skunk works), written by one of the original developers of XML, contains some proposals for what an XML 2.0 might look like: elimination of DTDs from syntax, integration of namespaces, XML Base and XML Information Set (infoset) into the base standard.

The World Wide Web Consortium also has a XML Binary Characterization Working Group doing preliminary research into use cases and properties for a binary encoding of the XML infoset. The working group is not chartered to produce any official standards. Since XML is by definition text-based, Sun Microsystems has proposed the name fast infoset for its own binary infoset to avoid confusion, and is working to standardize that through ISO.

See also

References

XML