W3C

SPARQL Query Results XML Format

W3C Working Draft 27 May 2005

This version:
http://www.w3.org/TR/2005/WD-rdf-sparql-XMLres-20050527/
Latest version:
http://www.w3.org/TR/rdf-sparql-XMLres/
Previous version:
http://www.w3.org/TR/2004/WD-rdf-sparql-XMLres-20041221/
Editor:
Dave Beckett, ILRT University of Bristol

Abstract

RDF is a flexible, extensible way to represent information about World Wide Web resources. It is used to represent, among other things, personal information, social networks, metadata about digital artifacts like music and images, as well as provide a means of integration over disparate sources of information. A standardized query language for RDF data with multiple implementations offers developers and end users a way to write and to consume the results of queries across this wide range of information.

This document describes an XML format for the variable binding and boolean results formats provided by the SPARQL query language for RDF, developed by the W3C RDF Data Access Working Group (DAWG), part of the Semantic Web Activity as described in the activity statement .

Status of this Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This is the second Public Working Draft of the SPARQL Variable Binding Results XML Format produced by the RDF Data Access Working Group, (part of the Semantic Web Activity) for review by W3C Members and other interested parties. The working group has moved toward an XML query results language that is conveniently described with XML Schema. No large changes are expected for the last call version of this document. Please send comments to the public public-rdf-dawg-comments@w3.org, a mailing list with a public archive. The query result format defined by this document is intended to make RDF query results conveniently available to XML applications. The working group would appreciate feedback and suggestions from developers of XPath and SAX applications.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced under the 5 February 2004 W3C Patent Policy. The Working Group maintains a public list of patent disclosures relevant to this document; that page also includes instructions for disclosing [and excluding] a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) with respect to this specification should disclose the information in accordance with section 6 of the W3C Patent Policy.

Per section 4 of the W3C Patent Policy, Working Group participants have 150 days from the title page date of this document to exclude essential claims from the W3C RF licensing requirements with respect to this document series. Exclusions are with respect to the exclusion reference document, defined by the W3C Patent Policy to be the latest version of a document in this series that is published no later than 90 days after the title page date of this document.


Table of Contents

1 Introduction

This document defines a format for encoding variable binding results and boolean results made by the SPARQL Query Language for RDF [SPARQL-QUERY] in XML [XML].

2. Definition

SPARQL Query defines several query result forms (SPARQL Query section 10) and this document defines an XML serialisation to encode variable bindings Query Results from SELECT (SPARQL Query section 10.2). and returning a boolean result from ASK (SPARQL Query section 10.5)

2.1. Document Element

The XML format has document element sparql element in the http://www.w3.org/2001/sw/DataAccess/rf1/result2 namespace, written as follows:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2">
 ...
</sparql>

Inside the sparql element are two sub-elements, head and results which must appear in that order.

2.2. Header

The head element is the first child element of the sparql element.

For a variable binding query result, head contains a sequence of elements describing the set of Query Variable (SPARQL Query section 4) names in the Query Results.

For a boolean query result, head has no content.

The order of the variable names in the sequence is the order of the variable names given to the argument of the SELECT statement in the SPARQL query. If SELECT * is used, the order of the names is undefined. The order used inside head must be the same as that used in the result elements, described below.

Inside the head element, the ordered sequence of variable names chosen are used to create empty child elements variable with the variable name as the value of an attribute name giving a document like this:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2">

  <head>
    <variable name="x"/>
    <variable name="hpage"/>
    <variable name="name"/>
    <variable name="mbox"/>
    <variable name="blurb"/>
  </head>
...
</sparql>

2.3. Query Results

The results element is the second child-element of sparql and must appear after head. It is written even if the Query Results is empty.

2.3.1. Variable Binding Results

For each Query Solution (SPARQL Query section 2.5) in the Query Results, a result child-element of results is added giving a document something like:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2">
  ...  head ...

  <results>
    <result>...
    </result>
    <result>...
    </result>
    ...
  </results>

</sparql>

Each result element corresponds to one Query Solution in an unordered result set (see 2.3.3 Ordering Results), and contains child elements for each Query Variable that appears in the solution (ordered the same as the variable child elements of head) and used to record how the query variable binds to an RDF Term (SPARQL Query section 4).

Each binding inside a solution is written as an element binding as a child of result with the query variable name as the value of the name attribute. So for a result binding two variables x and hpage it would look something like:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2">
  ...  head ...

  <results>
    <result>
      <binding name="x"> ... </binding>
      <binding name="hpage"> ... </binding>
    </result>

    <result>
      <binding name="x"> ... </binding>
      <binding name="hpage"> ... </binding>
    </result>
    ...
  </results>

</sparql>

The value of query variable binding which is either an RDF Term or unbound is written as the content of the binding as follows:

No binding
<binding><unbound/><binding>
RDF URI Reference U
<binding><uri>U</uri><binding>
RDF Literal S
<binding><literal>S</literal><binding>
RDF Literal S with language L
<binding><literal xml:lang="L">S</literal><binding>
RDF Typed Literal S with datatype URI D
<binding><literal datatype="D">S</literal><binding>
RDF Typed Literal S with datatype URI D and optional XSI type I
<binding><literal datatype="D" xsi:type="I">S</literal><binding>
When an RDF Typed Literal with a datatype URI D is used, an XML Schema datatype identified by an XML Qname may also be given. This must match the RDF datatype URI. Also note that the XSI namespaces and prefixes must be declared in the XML such as with:
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xsi:schemaLocation="http://www.w3.org/2001/sw/DataAccess/rf1/result2.xsd">
...
The xsi:schemaLocation is optional but may be needed for schema validation.
Blank Node label I
<binding><bnode>I</bnode><binding>

Note: The blank node label I is scoped to the result set XML document and does not have any association to the the blank node label for that RDF Term in the query graph.

A query solution encoded in this format could look something like the following:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2">
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xsi:schemaLocation="http://www.w3.org/2001/sw/DataAccess/rf1/result2.xsd">
  ...  head ...

  <results>

    <result> 
      <binding name="x"><bnode>r2</bnode></binding>
      <binding name="hpage"><uri>http://work.example.org/bob/</uri></binding>
      <binding name="name"><literal xml:lang="en">Bob</literal></binding>
      <binding name="mbox"><uri>mailto:bob@work.example.org</uri></binding>
      <binding name="age"><literal xsi:type="xs:integer" datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal></binding>
      <binding name="blurb"><unbound/></binding>
      <binding name="friend"><bnode>r1</bnode></binding>
    </result>

    ...
  </results>

</sparql>

2.3.2. Boolean Results

The boolean result is written as the element content of a boolean child-element of the results element, either as true or false as follows:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2">
  ...  head ...

  <results>
    <boolean>true</boolean>
  </results>

</sparql>

2.3.3. Ordering Results

If the variable binding results are ordered such as with the SPARQL ORDER BY clause, an index attribute with a positive decimal integer may be given on the result element as follows:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result2">
  ...  head ...

  <results>
    <result index="1">
       ...
    </result>

    <result index="2">
       ...
    </result>
    ...
  </results>

</sparql>

The index numbers monotonically increase in document order. That is, later index numbers are always equal to or greater than earlier index numbers. Applications that read the XML document in parts using streaming can rely on this.

3. Examples

3.1. Variable Binding Results Examples

An example SELECT SPARQL Query in example.rq operating on query graph Turtle/N3 data in data.n3 provides variable binding query results written in XML in output.xml. following the definition in section 2

This XML can be transformed into XHTML using the sample XML Query script result2-to-html.xq giving output-xquery.html or with XSLT sheet result2-to-html.xsl giving output-xslt.html

3.2. Boolean Results Examples

An example ASK SPARQL Query in example2.rq operating on query graph Turtle/N3 data in data.n3 provides a boolean query result written in XML in output2.xml. following the definition in section 2

4. XML Schemas

There are XML schemas for this format in the following formats:

These schemas are for information only.

5. References

[XML]
Extensible Markup Language (XML) 1.0, Third Edition, T. Bray, J. Paoli, C.M. Sperberg-McQueen, E. Maler, F. yergeau, Editors. World Wide Web Consortium. 4 February 2004. This version is http://www.w3.org/TR/2004/REC-xml-20040204. The latest version of XML is available at http://www.w3.org/TR/REC-xml.
[SPARQL-QUERY]
SPARQL Query Language for RDF, E. Prud'hommeaux, A. Seaborne, Editors. World Wide Web Consortium. 19 April 2005. Work in progress. This version is http://www.w3.org/TR/2005/WD-rdf-sparql-query-20050419/. The latest version of SPARQL Query Language for RDF is available at http://www.w3.org/TR/rdf-sparql-query/.
[RELAXNG]
RELAX NG Specification, James Clark and MURATA Makoto, Editors, OASIS Committee Specification, 3 December 2001. This version of RELAX NG is http://www.oasis-open.org/committees/relax-ng/spec-20011203.html. The latest version of the RELAX NG Specification is at http://www.oasis-open.org/committees/relax-ng/spec.html.
[RELAXNG-COMPACT]
RELAX NG Compact Syntax, James Clark, Editor. OASIS Committee Specification, 21 November 2002. This document is http://www.oasis-open.org/committees/relax-ng/compact-20021121.html.

A. Changes

2005-04-26. Added editorial issues for 2.3.3 and 4

2005-04-25. Switched namespace prefixes to http://www.w3.org/2001/sw/DataAccess/rf1/result2
Added bindings element with sub-elements bnode, uri and literal.
Added optional xsi:type for datatyped literals
Updated XSLT and XQuery scripts to match new XML syntax.
Point to SPARQL Query WD of 19 April 2005.
2.3.3 Ordering Results New section describing how to record ordered results.

2005-02-28. Title change to: SPARQL Query Results XML Format
Update references to 2005-02-17 SPARQL Query WD.
1, 2. Expand to mention boolean results.
2.2 Describe vb and boolean result formats.
2.3.1 New section containing VB results content.
2.3.2 New section describing boolean-result element.
3. Added sections 3.1 and 3.2 for variable binding results and boolean results examples. Added links to example2.rq and output2.xml
Updated RelaxNG and derived schemas to add boolean-result element
Updated references.

2005-02-15. Define the order of the variables names in the header as that in the SELECT in the query, or undefined for SELECT *.