XaLib |
SPARQL Query Engine |
Simple SPARQL example.
The following example shows the code needed for a simple example:
1.
Include XaLib in your XUL file.
<script type="application/x-javascript"
src="chrome://xalib/content/XaLib.js"/>
2.
Include the Query Engine in your Javascript code.
XaLib.include("chrome://xalib/content/RDF/SPARQL/Query.js");
3 Declare variables used in
the query:
var _subject = new Var("Subject");
var _predicate = new Var("Predicate");
var _object = new Var("Object");
4.
Construct the query using a Javascript object literal:
Query Literal syntax details can be found here.
var q;
eval (
q = new Query({
// The
Prefix clause defines a
set of namespace prefixes used to shorten the form of RDF resources.
Prefix:
{ext:"http://www.mozilla.org/2004/em-rdf#"},
// The
Select clause indicates
the variables to be included in the results.
Select:
[_subject, _predicate,
_object],
// The
From clause gives a list
of RDF files to be queried.
// The dir: protocol is an
XaLib way to
access well know directories.
From:
["dir://profile/extensions/extensions.rdf"],
// The Where clause
specifies which data is to be returned by the query.
Where: [
Triple(_subject, _predicate,
_object)
]
});
);
5.
Execute the query.
q.compile();
q.execute();
6 Iterate through the results:
var itr = q.results();
var result;
// (The double parentheses stop
strict mode warnings.)
while ((result = itr() )) {
// The result property names relate to
the names
passed to the Var constructors above.
alert("Subject:" + result.Subject + " Pedicate:" +
result.Predicate + " Object:" +
result.Object);
}