GOAL
results {
all result { var TITLE, var AUTHOR }
}
FROM
in {
resource { "file:bib.xml", "xml" },
bib {{
book {{
var TITLE -> title {{ }},
var AUTHOR -> author {{ }}
}}
}}
}
END
The query part in this example consists of a single query (we shall see later how to query several different resources in one rule) to the resource "bib.xml", intending to bind the variables TITLE and AUTHOR to title and author elements below the same book.
Resource Specification. An input resource is specified by enclosing the query in an in element that contains (at least) one resource specification. In the above case, the input resource associated with the query is the file "bib.xml" in format "xml". Besides "file", Xcerpt also supports "http". Other supported formats for querying currently include "xcerpt" (databases in Xcerpt's native syntax) and "html" (using a slightly more tolerant parser to parse HTML documents).
Query Terms. A pattern selection for binding variables is specified by an query term. In Xcerpt's native syntax, a query term is defined as follows:
In the above case, the query term is
bib {{
book {{
var TITLE -> title {{ }},
var AUTHOR -> author {{ }}
}}
}}
It queries for a bib element that contains a book element (put possibly other book or non-book elements as well, due to the double braces) which in turn contains two elements title and author (and possibly other elements) in any order and binds those two elements to the variables TITLE and AUTHOR.
Obviously, there exist several valid combinations of the variables TITLE and AUTHOR, as there are several books and books may contain more than one author element.
If the query for book would be specified with single curly braces, the query term would only match such books that contain exactly one title and one author element (in any order) but no other authors and no other kinds of elements (like price).
A construct pattern specifies how the variable bindings resulting from evaluating the query part are rearranged. A construct term is similar to a query term, but may contain neither double braces (as they specify partial content, but a result is never partial) nor variable restrictions (a restriction is part of querying). Furthermore, construct terms may contain the construct all to indicate a collection of all possible variable bindings. The construct term of the example above is:
results {
all result { var TITLE, var AUTHOR }
}
This construct term creates an XML document with root element results and as children all instances of result elements such that there are valid combinations (i.e. such titles and authors that are below the same book) of bindings for the variables TITLE and AUTHOR.
Using different nestings of the all construct it is possible to create all many of results with the different valid variable bindings, e.g. the a list of all titles for each author:
results {
all result { all var TITLE, var AUTHOR }
}
Or you could want a list of all authors for a title:
results {
all result {var TITLE, all var AUTHOR }
}