XML Primer

Imran Rashid

DRAFT

Introduction to XML

XML, or the eXtensible Markup Language, is a set of syntactic rules for storing structured data in text format.
It is a meta-language — it only defines rules for making up other languages.

You can make up whatever tag names you want.

example

Might seem similar to HTML, but its not HTML.

Basic set of rules

Exactly one root element

Notice the "wrapper" element around all the addresses.

This is wrong:

Balanced start-tags and end-tags

Every start-tag must have a matching end tag.

This is wrong:

Proper nesting

The tags must be nested in the proper order.

This is wrong:

Case sensitive

start-tags and end tags must use the same capitalization

This is wrong:

New syntax for empty element

If an element has no content, there is a special representation for empty elements.  There is a '/' at the end of the tag.

This is equivalent to

This is wrong:

Attributes must have values

This is wrong:

Attribute values must be quoted

This is wrong:

Examples of XML formats

Since it is a meta-language, there are many "formats".

(these examples have been trimmed.)

GeML

Gene Expression Markup Language

MoDL

Molecular Dynamics Language.  Used to define visualizations of molecular models.

BSML

Bioinformatic Sequence Markup Language

BioMed

XBRL

Used for Accounting Information

What exactly is XML?

XML is a product of the W3C (World Wide Web Consortium).

It became a W3C recommendation on Feb 10, 1998. It built off of previous work in SGML.  However, SGML had proved to be very unwieldy — XML set out to build off experience with SGML, but make something easier to use.

XML builds off of SGML, but is easier to use.

Elements and attributes are the most basic syntactic constructs

A few other basic datatypes such as comments and the xml declaration.

Easy to read and write by a computer

Uses Unicode as the underlying character set.

DTD's

Document Type Definition — a basic grammar for your specific XML format.  The head of an XML document will point to the location for a DTD.

The file referenced uses a special to define the grammar:

This is not XML syntax.

No type constraints or datatyping.

Very weak — well known that it cannot express many constraints.

Well-formed vs. Valid

SGML required every document to have a DTD — XML got rid of that requirement!  An xml file can be parsed without knowing anything about the DTD.

Perfectly OK XML file.

If an XML file meets the basic syntactic requirments, its well-formed.

If it also references a DTD, and it is OK according to that DTD, than it is valid.

so given this DTD:

this XML file is invalid, but well-formed:

Namespaces in XML

Avoiding name conflicts

is this part of a Notebook Expresssion or is this in a biomedical context?

Basic idea: tie each element to a URI

Important thing is the URI, not the prefix

These two are the same

Where are Namespaces used?

Namespaces do not have to be used!  For very simple applications, if you aren't worried about interfering with other XML formats, you don't need to worry about namespaces.  Even if you are using an XML format which uses namespaces, often it will use the same namespace throughout, so a programmer might not need to worry about them much.

In fact, it is worth pointing out that Namespaces are not part of XML 1.0.  They were introduced almost one year later.  Many XML frameworks were built in that framework, and some continue to operate today.

However, namespaces have proved very useful for many applications.  For a serious XML developer that is combining several XML formats or if they are working with other who might have their own XML formats, namespaces have proven to be very useful.

What resource does a namespace point to?

Because Namespaces are URL's, a common assumption is that if you dereferene the URL there is something special there.

But, the URL is just a unique string.  An XML processor never needs to see what is attached to the URL — all it cares about is the fact thats its a String.

Of course, this doesn't change the fact that people will try to dereference the URL, just to see what there is.  There really is no wide-spread standard on what you should put at the URL, though it seems to be common practice to put a human-readable description of the namespace.

An interesting development out of the XML developer community is RDDL (Resource Directory Description Language).  It is a way to use XML to embed computer-processable information inside a human-readable document.  It still remains to be seen whether it will find widespread support, but its definitely a very interesting development.

http://www.rddl.org

Advantages of XML

Extensible

Not constrained to a set of labels.

Anybody can make up their own element names.

Organizations can then work together to standardize similar labels.

Not Propietary

XML is a free, open standard.

Text format is easier to understand then a proprietary binary format.

Ubiquitous

Widely supported

Cross platform

Cross language

International

Many free open source implementations

"buzz word" & band wagon effect

XML builds off of Unicode

ALL parsers are required to support unicode.

Unicode is great for Internationalization.

Three basic ideas

XML is an open standard, platform-independent, internationalized, and is well-supported.

Elements & attributes form the basis of XML syntax.

When processing XML, work on the underlying tree-structure.

General XML Design Philosophies

Documents vs. Data

XML can represent any tree-structured data.

Good for documents (like HTML)

Good for data sets (gene sequencing or astronomical information)

Seperation of Content From Presentation

Good design philosophy in general

The same content may be presented in multiple formats

XML makes this easier to do

Mathematica Notebooks already allow a seperation of content and presentation

(Semi-)Human Readable

Consider this example:

From the computers standpoint, it is basically equivalent to this:

While XML files aren't intended to read directly, by using sensible names, they make debugging much easier for developers.

XML & Programming

General Idea of Parsing an XML document

XML file is parsed

Parser returns a tree-model which is exposed to other applications

XML applications dont' worry about syntactic details

SAX & DOM

Two standard APIs for processing XML

DOM — Document Object Model.  This builds an in-memory tree from the XML document.  Every element, every attribute, every text segment is a node of the tree.  DOM Level 1 & Level 2 are recommendations, and DOM Level 3 is still a work in progress.

SAX — Simple API for XML.  Provides a very straight-forward stream-based interface to your XML document.  Note that this is NOT a product of the W3C — a product of the developer community.   But it has very widespread support.

Many Other Possibilities

Parsers have internal APIs

Specialty Tools

XML & The Web

XML + CSS

Cascading StyleSheets (CSS)

Defines simple layout properties for XML elements.

example CSS stylesheet:

CSS Levels One and Two are W3C recommendations, Level 3 is in progress.

XML + XSLT

XSLT is a transformation language (more later).

Web server delivers XML + XSLT

XSLT is more powerful than CSS

XSLT is harder to learn than CSS

The browser will transform to HTML

Doesn't work in all the browsers yet (IE 5.5, Mozilla)

Simple XSLT example

XML source:

XSLT code:

Resulting HTML

XML On The Server Side

XML can be used on the server, no matter what web browsers clients are using.

The web server can dynamically transform the XML documents to HTML for the client.

Web server can use any XML tool it wants — XSLT, Java, etc.

Why bother?

Why not just use HTML in the first place?

Seperation of content from presentation

The underlying XML may be queiried in a search

Allows presentation in a variety of formats

XML-Related Specifications

XHTML

Reformulation of HTML in XML syntax

WML

Wireless Markup Language

Part of the WAP (Wireless Application Protocol), industry standard including groups from Ericcson, Nokia, and Motorola

XSLT

eXtensible Stylesheet Language Transformations

A programming language in XML syntax.

Used for transforming one XML document into another.  (But there are many other languages used for this.)

Supported by some web browsers.

SVG

Scalable Vector Graphics — a vector graphic format which is expressed in XML.  Includes primitives for points, lines, curves, animations and more.

"PostScript for the Web"

MathML

"MathML is a low-level specification for describing mathematics as a basis for machine to machine communication. It provides a much needed foundation for the inclusion of mathematical expressions in Web pages"

XML Schemas

Validation of XML documents

Provides data-typing

More powerful than DTDs

competition

Schematron

Relax NG

RELAX NG is maintained by the OASIS group, another committee that works on industry standards, similar to the W3C in many ways.

Many more...

Different Types of XML Dialects

W3C Standard

Official Stamp of approval from the W3C

MathML, SVG, XSLT, and others

Industy Standard

Different companies in the same field work together to establish a common XML format.

Open Vendor Specific Format

XML format specific to one vendor's product

Specification is made open to public

Allows interchange with other XML formats

XML files can be manipulated with XML tools

Private formats

Not used for interchange, simply used for stability of XML

Mathematica's XML Functionality

Other talks at the conference will discuss details of what Mathematica can do with XML

NotebookML

NotebookML is a loss-less format for saving Mathematica Notebooks in XML

The talk will focus on how you use NotebookML, what its good for, and how it is related to XML

MathML

More detailed explanation of MathML

Look at our support for MathML

The status of MathML, including a look at support in other software

SymbolicXML

JLink and XML

Java and XML are a natural match

Examine some ways to link the extensive Java XML APIs into Mathematica's own XML handling capabilities

Use of SAX & DOM

Using XSLT within Mathematica

Processing XML data available on the web

Scratching the Surface

Much more to XML — many links for more info.

Three basic ideas

XML is an open standard, platform-independent, internationalized, and is well-supported.

Elements & attributes form the basis of XML syntax.

When processing XML, work on the underlying tree-structure.

XML Resources

For those interested in more information, these resources provide a lot of good information.

XML Cover Pages

Zvon Tutorials

XML-DEV List

XSL-List