Copyright © 2007 W3C® ( MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark, and document use rules apply.
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.
This is the work of the author — it is not endorsed by the W3C members.
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.
The following table defines the namespace prefixes used in this document and describes their purposes.
prefix | description | namespace |
---|---|---|
spat | Namespace for SPARQL Annotations | http://www.w3.org/2007/01/SPAT/ns |
xb | example namespace for XML Books documents | http://example.org/schemas/XML/Books/ |
rb | example Books namespace in RDF | http://example.org/schemas/RDF/Books# |
RDF Graphs are represented in the RDF Graph language TURTLE [TURTLE].
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:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://example.org/schemas/XML/Books/" xmlns:xb="http://example.org/schemas/XML/Books/"> <xs:element name="BookList"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" ref="xb:Book"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Book"> <xs:complexType> <xs:sequence> <xs:element name="Title" type="xs:string"/> <xs:element name="Publisher" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
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("xb:Title") ; rb:publisher xpath("xb:Publisher") ] .">
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:
xmlns:spat="http://www.w3.org/2007/01/SPAT/ns" xmlns:rb="http://example.org/schemas/RDF/Books#"
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.
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" ] .
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: <http://example.org/schemas/RDF/Books#> . [ 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="http://example.org/schemas/XML/Books/"> <xb:Book> <xb:Title>Hare-brained XML</xb:Title> <xb:Publisher>Carrion Brothers</xb:Publisher> </xb:Book> <xb:Book> <xb:Title>Semweb Chicanery</xb:Title> <xb:Publisher>Carrion Brothers</xb:Publisher> </xb:Book> </xb:BookList>