SPARQL Query Results XML Format

W3C Working Draft 1 August 2005

This version:
Previous version:
Latest version:
Dave Beckett, Institute for Learning and Research Technology (ILRT), University of Bristol


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 is a Last Call Working Draft of the SPARQL Query Results XML Format. It was ratified without objection within the RDF Data Access Working Group (DAWG), part of the Semantic Web Activity. This query results format is intended to be conveniently processable with XQuery and XSLT, and thus make RDF query results available to XML applications. SPARQL Query Language for RDF is in a last call and SPARQL Protocol for RDF is expected to reach last call in the coming weeks. Both are expected to finish the comments period on 1 September 2005. Please send comments to the public public-rdf-dawg-comments@w3.org, a mailing list with a public archive.

Comments on this document are due 1 September 2005; please send them to public-rdf-dawg-comments@w3.org, a mailing list with a public archive.

The Changes appendix shows the differences between this document and the previous version.

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/.

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

The SPARQL Query Language for RDF [SPARQL-QUERY] defines several Query Result Forms (SPARQL Query section 10). This document defines a SPARQL Results Document that encodes the variable binding query results from SELECT queries (SPARQL Query section 10.2) and boolean query results from ASK queries (SPARQL Query section 10.5) in XML [XML].

2. Definition

Definition: SPARQL Results Document

A SPARQL Results Document is an XML document that is valid with respect to either the RELAX NG XML Schema or the W3C XML Schema in Section 4.

2.1. Document Element

The SPARQL Results Document begins with sparql document element in the http://www.w3.org/2005/sparql-results# namespace, written as follows:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">

Inside the sparql element are two sub-elements, head and a results element (either results or boolean) 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 must contain a sequence of elements describing the set of Query Variable names in the Solution Sequence (here called query results).

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/2005/sparql-results#">

    <variable name="x"/>
    <variable name="hpage"/>
    <variable name="name"/>
    <variable name="mbox"/>
    <variable name="blurb"/>

For a boolean query result, no elements are required inside head and variable must not be present.

For any query result, head may also contain link child elements with an href attribute containing a relative URI that provides a link to some additional metadata about the query results. The relative URI is resolved against the in-scope base URI which is usually the query results format document URI. link elements must appear after any variable elements that are present.

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">

    <link href="metadata.rdf"/>

2.3. Results

The second child-element of sparql must appear after head and is either results or boolean. It is written even if the query results are empty.

2.3.1. Variable Binding Results

The results element has 2 required attributes which both take boolean values: either true or false.

For each Query Solution in the query results, a result child-element of results is added giving a document like:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  ...  head ...

  <results ordered="false" distinct="false">


Each result element corresponds to one Query Solution in a result 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 variables binds to RDF Terms.

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 like:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  ...  head ...

  <results ordered="false" distinct="false">
      <binding name="x"> ... </binding>
      <binding name="hpage"> ... </binding>

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


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

No binding
RDF URI Reference U
RDF Literal S
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>
Blank Node label I

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

An example of a query solution encoded in this format is as follows:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  ...  head ...

  <results ordered="true" distinct="false">

      <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 datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal></binding>
      <binding name="blurb"><unbound/></binding>
      <binding name="friend"><bnode>r1</bnode></binding>



2.3.2. Boolean Results

A boolean result is written as the element content of a boolean child-element of the sparql element directly after a head, containing either true or false as follows:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  ...  head ...



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 providing ordered variable binding query results written in XML in output.srx.

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.srx.

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

4. XML Schemas

There are normative XML schemas provided in the following formats:

  1. RELAX NG[RELAXNG] Compact[RELAXNG-COMPACT] in result2.rnc
  2. RELAX NG XML in result2.rng
  3. W3C XML Schema[XMLSCHEMA-1] in result2.xsd
    Note: this schema is machine-generated from the RELAX NG XML schema.

If W3C XML Schema is used, an xsi:schemaLocation attribute can be used pointing to the schema as follows:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#"



5. Internet Media Type, File Extension and Macintosh File Type

The Internet Media Type / MIME Type for the SPARQL Query Results XML Format is "application/sparql-results+xml".

It is recommended that result files have the extension ".srx" (all lowercase) on all platforms.

It is recommended that result files stored on Macintosh HFS file systems be given a file type of "TEXT".

This information that follows is intended to be submitted to the IESG for review, approval, and registration with IANA.

Internet Media Type Registration Form

To: ietf-types@iana.org
Subject: Registration of media type application/sparql-results+xml

Type name:
Subtype name:
Required parameters:
Optional parameters:
"charset": This parameter has identical semantics to the charset parameter of the "application/xml" media type as specified in [RFC3023], section 3.2.
Encoding considerations:
Identical to those of "application/xml" as specified in [RFC3023], section 3.2.
Security considerations:

SPARQL query results uses URIs. See Section 7 of [RFC3986].

SPARQL query results uses IRIs. See Section 8 of [RFC3987].

As this media type uses the "+xml" convention, it shares the same security considerations as described in [RFC3023], section 10.

Interoperability considerations:
There are no known interoperability issues.
Published specification:
This specification.
Applications which use this media type:
No known applications currently use this media type.
Additional information:
Magic number(s):
As specified for "application/xml" in [RFC3023], section 3.2.
File extension(s):
Fragment identifiers:
Identical to that of "application/xml" as described in RFC 3023 [RFC3023], section 5.
Base URI:
As specified in [RFC3023], section 6.
Macintosh file type code(s):
Person & email address to contact for further information:
Dave Beckett, Eric Prud'hommeaux <public-rdf-dawg-comments@w3.org>
Intended usage:
Restrictions on usage:
Author/Change controller:
The SPARQL specification is a work product of the World Wide Web Consortium's RDF Data Access Working Group. The W3C has change control over these specifications.


[RFC3023] Murata, M., St. Laurent, S., and D. Kohn, "XML Media Types", RFC 3023, January 2001.

[RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform Resource Identifier (URI): Generic Syntax", STD 66, RFC 3986, January 2005.

[RFC3987] Duerst, M. and M. Suignard, "Internationalized Resource Identifiers (IRIs)", RFC 3987, January 2005.

6. References

Extensible Markup Language (XML) 1.0, Third Edition, T. Bray, J. Paoli, C.M. Sperberg-McQueen, E. Maler, F. Yergeau, Editors, W3C Recommendation, 4 February 2004. This document is http://www.w3.org/TR/2004/REC-xml-20040204 . The latest version is available at http://www.w3.org/TR/REC-xml .
SPARQL Query Language for RDF, E. Prud'hommeaux, A. Seaborne, Editors, W3C Working Draft (work in progress), 21 July 2005. This document is http://www.w3.org/TR/2005/WD-rdf-sparql-query-20050721/ . The latest version is available at http://www.w3.org/TR/rdf-sparql-query/ .
RELAX NG Specification, James Clark and MURATA Makoto, Editors, OASIS Committee Specification, 3 December 2001. This document is http://www.oasis-open.org/committees/relax-ng/spec-20011203.html . The latest version is available at http://www.oasis-open.org/committees/relax-ng/spec.html .
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 .
XML Schema Part 1: Structures Second Edition, D. Beech, N. Mendelsohn, M. Maloney, H. S. Thompson, Editors, W3C Recommendation, 28 October 2004. This document is http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/ . The latest version is available at http://www.w3.org/TR/xmlschema-1/ .

A. Changes (Informative)

2005-07-28. Changes after review by Dan Connolly:
Section 2.3.1. Blank nodes labels need not have an association to the blank node labels in the graph (comment 0129)
Section 5. Added EricP to contact info, changed email to comments list (comment 0130)
Section 5. Deleted citation and references to XML encoding, digital signatures in MIME registration form (comment 0130)
Changed html style of references to terms defined in SPARQL Query such as Query Variable, Query Solution and RDF Term to link directly to the definition.
Use definition Solution Sequence and turn Query Results into a phrase throughout.
Sections 1 and 2. Made the definition of SPARQL Results Document more declarative and merged the remaining text into section 1. (comment 0131)
Section 4. Cite XML Schema-1 normatively for the W3C XML Schema. (comment 0131)
Section 6. Add XML Schema-1 reference. (comment 0131)
Section 6. Tidy the references HTML formatting. Section 3. Use .srx suffix for example files.

2005-07-26. For a boolean query result, variable must not be present.
Section 3.2. Added XHTML results made by XQuery and XSLT from the boolean format output
Update pointers to terms in the SPARQL Query working draft to use definitions anchors and correct sections
Added section 5. Internet Media Type, File Extension and Macintosh File Type with suggested file extension ".srx" after 2005-07-26 WG decision
Switched XML namespace to http://www.w3.org/2005/sparql-results# after 2005-07-26 WG decision
Section 4. Made both XML schemas normative after 2005-07-26 WG decision
Section 2. Added a definition of a SPARQL Results Document after 2005-07-26 WG decision
Some minor rewordings

2005-07-25. Changes after reviews by Jeen Broekstra and Lee Feigenbaum made on 2005-07-22 mostly wording changes but also:
Updated references to point to SPARQL Query Language 21 July 2005 Working Draft
Allow link to be repeated

2005-07-21. Added sections 2.1, 2.2 and 2.3 to table of contents
Section 2.3 is now titled Results
Added ordered and distinct required attributes of the results element for variable binding results, recording equivalent information to SPARQL query ORDER BY and DISTINCT respectively.
Deleted section 2.3.3 Ordered Results as a consequence of the above changes.
Updated examples, XSLT, XQuery and generated XHTML to show using the distinct and ordered attributes.

2005-07-13. Removed xsi:type from datatyped literals as W3C XML Schemas could not record this as an optional attribute on an element with literal content. Optional attributes require a ComplexType however xsi:type is only allowed on a SimpleType.
Moved boolean (boolean results) to be an alternate for results (variable binding results) to allow earlier detection of the results format after feedback from Andy Seaborne.
Added link to allow linking to metadata about the query results after comment by Ian Harrison
Switched XML namespace to http://www.w3.org/2005/06/sparqlResults to match W3C policy for XML namespace names
Added publication note to switch result2.xsd to final location

2005-05-27. Published as SPARQL Query Results XML Format, W3C Working Draft 27 May 2005

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 *.

2004-12-21. Published as SPARQL Variable Binding Results XML Format, W3C Working Draft 21 December 2004

2004-12-20 variables element renamed to head
Replaced variables child elements <foo/> with <variable name="foo"/>
Made unbound variables appear as <var bound="false"/>

2004-12-15 Document element now <sparql>
Attribute id now bnodeid
Unbound query variables are omitted from a solution.
Noted that bnodeid values are scoped to the XML result document.

2004-12-13 Added definitions section
Switched href to uri as agreed 2004-09-29

2004-12-08 Initial version. Based on Updated DAWG variable binding results XML format, 2004-11-26 and draft XML query results format spec, 2004-09.28