SPAT — SPARQL Annotations

Work in progress.
$Revision: 1.10 $ of $Date: 2007/02/10 02:26:46 $
Eric Prud'hommeaux, W3C <>
related technologies: GRDDL · RDAL · SPDL


This document describes SPAT, the SPARQL Annotations language, an extension of the SPARQL Triple Pattern. SPAT extends a part of the SPARQL Query Language to include XPaths, in order to facilitate the flow of data between the semantic web and XML.

Status of this Document

This is the work of the author — it is not endorsed by the W3C members.

Table of Contents

1 Introduction

Developers of semantic web applications are often confronted with a need to access data in XML. Because this data is in a radically different model, it has been difficult to query using the RDF query language SPARQL [SPARQL]. For the same reason, data from the semantic web is tedious to place in XML documents, generally requiring construction of an XSLT stylesheet to transform the data either from RDF XML or SPARQL XML Results [RESULTS] to the desired XML schema.

SPARQL Annotations offers a solution — enabling simple associations between RDF terms and information in an XML document by adding XPaths [XPath] to SPARQL triple patterns. SPARQL Annotations are evaluated in the context of an XML document, allowing the direct transfer of data between that document and the RDF triples described by the triple pattern.

SPARQL Annotation can be viewed as an enabling technology. Any functionality is derived from applications using SPARQL Annotations. In this document, we will assume a simple XSLT-like application with inputs of an RDF graph BooksGraph, an either an XML document BooksDoc or a W3C XML Schema BooksSchema describing some class of documents. A second XML Schema BooksSchema-SPAT illustrates SPARQL Annotations in a schema file, and how that affects namespace and XPath resolution.

1.1 Document Convetions

The following table defines the namespace prefixes used in this document and describes their purposes.

prefix description namespace
spat Namespace for SPARQL Annotations
xb example namespace for XML Books documents
rb example Books namespace in RDF

RDF Graphs are represented in the RDF Graph language TURTLE [TURTLE].

2. Example

In order to illustrate SPARQL Annotations, consider an example:

[ rb:name      xpath("/xb:BookList/xb:Book/xb:Title") ;
  rb:publisher xpath("/xb:BookList/xb:Book/xb:Publisher") ] .

This example includes two triples patterns, associating the objects of the triples with some information accessible by the XPaths /xb:BookList/xb:Book/xb:Keywords and /xb:BookList/xb:Book/xb:SearchIndex. These XPaths match elements in a document described by this schema:

  <xs:element name="BookList">
        <xs:element maxOccurs="unbounded" ref="xb:Book"/>
  <xs:element name="Book">
        <xs:element name="Title" type="xs:string"/>
        <xs:element name="Publisher" type="xs:string"/>

3. Namespace and XPath Resolution

The resolution of XPaths and namespaces [NAMESPACE] is defined by the enabling technology. In our example application, namespaces come from xmlns declarations in the XML Schema document S and XPaths are resolved against the documents described by S, including the instance document D. For instance, the simple application described in this document is defined to resolve the SPAT XPaths against the root of the given (BooksDoc) XML document. If it is passed an XML schema with embedded SPARQL Annotations, it resolves them against the notional node described by that location in the XML Schema. For example, adding SPARQL Annotations to the Book element described by the above XML Schema

  <xs:element name="Book"
   spat:SPAT="[ rb:name      xpath(&quot;xb:Title&quot;) ;
	        rb:publisher xpath(&quot;xb:Publisher&quot;) ] .">

The namespaces in this SPARQL Annotation are resolved against xmlns declarations alread in the schema, plus extra ones added to handle the prefixes added in this annotation:


The XPaths are resolve against the location in the documents described by this schema. Thus, xb:Title is resolved relative to the xb:Book element.

4. Transferring XML Information to RDF

A simple SPARQL Annotation can be used to extract data from an XML document to construct triple pattern. The example SPARQL Annotations can be resolved against a document BooksDoc, conforming to the above schema, to produce the node xpath("xb:Title") => {"Hare-brained XML", "Semweb Chicanery"} and xpath("xb:Title") => {"Carrion Brothers", "Carrion Brothers"}. When substituted into the triple patterns, we get:

[ rb:name      "Hare-brained XML" ;
  rb:publisher "Carrion Brothers" ] .

[ rb:name      "Semweb Chicanery" ;
  rb:publisher "Carrion Brothers" ] .

5. Transferring RDF Information to XML

SPARQL Annotations can be used in conjunction with other document structure knowledge, for instance, XML Schema, to enable a processor to build a document with that structure, populated by data from an RDF graph, in this example, BooksGraph.

@prefix rb: <> .

[ rb:name      "Hare-brained XML" ;
  rb:publisher "Carrion Brothers" ] .

[ rb:name      "Semweb Chicanery" ;
  rb:publisher "Carrion Brothers" ] .

The XML Schema describes a document with the root <BookList>. The annotation on the element Book provides the necessary information to create a series of two <Book> elements. The document resulting from creating the maximum number of <Book> elements is:

<xb:BookList xmlns:xb="">
    <xb:Title>Hare-brained XML</xb:Title>
    <xb:Publisher>Carrion Brothers</xb:Publisher>
    <xb:Title>Semweb Chicanery</xb:Title>
    <xb:Publisher>Carrion Brothers</xb:Publisher>

A. References

SPARQL Query Language for RDF, E. Prud'hommeaux, A. Seaborne, Editors. World Wide Web Consortium. 19 April 2005. Work in progress. This version is The latest version of SPARQL Query Language for RDF is available at
XML Path Language (XPath) 2.0, Don Chamberlin , Anders Berglund, Scott Boag, et. al., Editors. World Wide Web Consortium, 23 Jan 2007. This version is The latest version is available at
"Turtle - Terse RDF Triple Language, Dave Beckett.
SPARQL Query Results XML Format , D. Beckett, Editor, W3C Working Draft (work in progress), 27 May 2005, . Latest version available at .
Namespaces in XML 1.1 , T. Bray, A. Layman, D. Hollander, R. Tobin, Editors, W3C Recommendation, 4 February 2004, . Latest version available at .