Intro to XML

XML is a language used to define structured data in a text file. This means it can be viewed and edited by people, and parsed by machines to share data or read input.

XML stands for Extensible Markup Language, which we will unpack with some examples.

XML Syntax

Elements

Here is a basic XML element. It is named "element" and contains the text "data".

<element>data</element>

Here is a more complex example that shows how we can organize information. Elements have a parent/child relationship with their containing element and the elements they contain.

<books>
  <book>
    <title>My first book</title>
    <summary>A book about Andrew Morton.</summary>
  </book>
  <book>
    <title>Intro to Drupal</title>
    <summary>Learn about using and building Drupal sites.</summary>
  </book>
</books>

Attributes

Elements can have attributes, which can be used to provide additional information about the contents.

Here is what an attribute looks like.

<book author="Andrew Morton">

Schema

Systems that use XML need to define a schema so the data can be correctly formatted by the source and parsed by the consumer.

There are decisions to make such as whether something should be an attribute or element.

For example, the author attribute could be another element instead.

<book>
  <title>My first book</title>
  <summary>A book about Andrew Morton.</summary>
</book>

XPath

XPath is a language for reading data within an XML document.

# get all books
//book

# get all books by andrew
//book[author="Andrew Morton"]

 

Questions