SPARQL 1.2 Query Results XML Format (original) (raw)
Abstract
This specification is published by theRDF Star Working Group as part of the update of specifications for format and errata.
Introduction
The [[[SPARQL12-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.
There are two other results formats which follow a similar design but do not use XML: [[[SPARQL12-RESULTS-JSON]]] and [[[SPARQL12-RESULTS-CSV-TSV]]].
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.
Document Element
The SPARQL Results Document begins with sparql
document element in the http://www.w3.org/2005/sparql-results#
namespace, written as follows:
...
Inside the sparql
element are two sub-elements, head
and a results element (either results
or boolean
) which must appear in that order.
If no literals with base direction appear in the results, the sparql
document element may be simplified as follows.
Different values of its:version
are allowed.
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.
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:
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 anyvariable
elements that are present.
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.
Variable Binding Results
The results
element contains the complete sequence of query results.
For each Query Solution in the query results, a result
child-element ofresults
is added giving a document like:
... head ...
... ... ...Each result
element corresponds to one Query Solution in a result and contains child elements (in no particular order) for each Query Variable that appears in the solution. It is used to record how the query variables bind 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:
... ...
<result>
<binding name="x"> ... </binding>
<binding name="hpage"> ... </binding>
</result>
...
The value of a query variable binding, which is an RDF Term, is included as the content of the binding
as follows:
RDF URI Reference U
<binding><uri>
U</uri></binding>
RDF Literal S
<binding><literal>
S</literal></binding>
RDF Literal S with language L without base direction
<binding><literal xml:lang="
L">
S</literal></binding>
RDF Literal S with language L with base direction L
<binding><literal xml:lang="
L" its:dir="
B">
S</literal></binding>
RDF Typed Literal S with datatype URI D
<binding><literal datatype="
D">
S</literal></binding>
Blank Node label I
<binding><bnode>
I</bnode></binding>
Triple Term, with subject S, predicate P, object O
<binding> <triple> <subject>
S</subject> <predicate>
P</predicate> <object>
O</object> </triple> </binding>
If, for a particular solution, a variable is unbound, no binding
element for that variable is included in the result
element.
S, P, and O in Triple Terms are encoded recursively, using the same format, without the enclosing <binding>
tag
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:
<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="age">
<literal datatype="http://www.w3.org/2001/XMLSchema#integer">30</literal>
</binding>
<binding name="mbox">
<uri>mailto:bob@work.example.org</uri>
</binding>
...
An example of a query solution that includes triple terms is as follows:
<result>
<binding name="x">
<bnode>r2</bnode>
</binding>
<binding name="name">
<literal xml:lang="en">Bob</literal>
</binding>
<binding name="triple">
<triple>
<subject>
<uri>http://example.org/alice</uri>
</subject>
<predicate>
<uri>http://example.org/name</uri>
</predicate>
<object>
<literal datatype="http://www.w3.org/2001/XMLSchema#string">Alice</literal>
</object>
</triple>
</binding>
</result>
...
As an alternative to including the `xml:its` declaration in every result set, the namespace can be declared on specific elements as needed:
قطة cat chatBoolean Results
A boolean result is written as the element content of a boolean
child-element of the sparql
element directly after a head
, containing eithertrue
or false
as follows:
true
Examples
Variable Binding Results Examples
An example SELECT
SPARQL Query in example.rq operating on query graph Turtle/N3 data in data.ttl 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 result-to-html.xq giving output-xquery.html or with XSLT sheet result-to-html.xsl giving output-xslt.html
Variable Binding Results Examples with Triple Terms
An example SELECT
SPARQL Query in example-triple-terms.rq operating on query graph Turtle/N3 data in data.ttl providing ordered variable binding query results written in XML in output-triple-terms.srx. These results contain triple terms.
Boolean Results Examples
An example ASK
SPARQL Query in example2.rq operating on query graph Turtle/N3 data in data.ttl provides a boolean query result written in XML in output2.srx.
This XML can be transformed into XHTML using the sample XML Query script result-to-html.xq giving output-xquery2.html or with XSLT sheet result-to-html.xsl giving output-xslt2.html
XML Schemas
There are normative XML schemas provided in the following formats:
- RELAX NG [[RELAXNG]] Compact [[RELAXNG-COMPACT]] in result.rnc
- RELAX NG XML in result.rng
- W3C XML Schema [[XMLSCHEMA-1]] in result.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:
...
Changes between SPARQL Query Results XML Format (Second Edition) and SPARQL 1.2 Query Results XML Format
- The document title changed from "SPARQL Query Results XML Format (Second Edition)" to "SPARQL 1.2 Query Results XML Format"
- Add RNC, RNG, and XSD files
- Allow triple terms to be expressed in
- Use Media Type language instead of MIME Type in
- Support directional language-tagged strings in
Internet Media Type, File Extension and Macintosh File Type
The Internet Media Type (formerly known as 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".
Privacy Considerations
TODO
Security Considerations
TODO
Internationalization Considerations
TODO