Skip to content

XQuery XQuery Update Facility (with temporal enhancements) usage

Johannes Lichtenberger edited this page Apr 5, 2018 · 1 revision

Use the doc(xs:string) function to open a document in it's latest version.

Use the doc(xs:string, xs:int) function to open a document in a specific version.

Furthermore XPath axis extensions exist: For instance next::, previous::, future::, future-or-self::, past::, past-or-self::, first::, last:: and all-time::.

With bit:serialize(xs:string) a document can be serialized, for instance bit:serialize(doc('foo')) serializes the document in it's up-to-date version (the most recent revision).

With something like String.format("bit:load('mydoc.xml', '%s')", doc); as the query one can load an XML-file into Sirix via XQuery. With something like String.format("bit:load('mydocs.col', io:ls('%s', '\.xml$'))", dir) a whole collection can be created given the data-directory with *.xml files.

A few code examples:

// Prepare sample document.
final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
final File doc = generateSampleDoc(tmpDir, "sample");
doc.deleteOnExit();

// Initialize query context and store (use Updating.YES if the store needs to be opened for updates (when using the XQuery Updating Facility).
try (final DBStore store = new DBStore(Updating.YES)) {
  final QueryContext ctx = new QueryContext(store);
  // Use XQuery to load sample document into store.
  System.out.println("Loading document:");
  final String xq1 = String.format("bit:load('mydoc.xml', '%s')", doc);
  System.out.println(xq1);
  new XQuery(xq1).evaluate(ctx);

  // Reuse store and query loaded document.
  final QueryContext ctx2 = new QueryContext(store);
  System.out.println();
  System.out.println("Update loaded document:");
  final String xq2 = "insert nodes <a><b/></a> into doc('mydoc.xml')/log";
  System.out.println(xq2);
  final XQuery q = new XQuery(xq2);
  q.serialize(ctx2, System.out);
  store.commitAll();
  System.out.println();
}
try (final DBStore store = new DBStore()) {
  final QueryContext ctx3 = new QueryContext(store);
  System.out.println();
  System.out.println("Query loaded document:");
  final String xq3 = "doc('mydoc.xml', 0)/log/all-time::*/*";
  System.out.println(xq3);
  final XQuery q = new XQuery(xq3);
  q.setPrettyPrint(true);
  q.serialize(ctx3, System.out);

  final QueryContext ctx4 = new QueryContext(store);
  final String xq4 = "bit:serialize(doc('mydoc.xml', 0))";
  q = new XQuery(xq4);
  try (final PrintStream out = new PrintStream(new FileOutputStream(
	new File(new StringBuilder(LOCATION.getAbsolutePath())
		.append(File.separator).append("output-revision-0.xml")
			.toString())))) {
    q.setPrettyPrint(true).serialize(ctx4, out);
  }
  System.out.println();
  final QueryContext ctx5 = new QueryContext(store);
  final String xq5 = "bit:serialize(doc('mydoc.xml', 1))";
  q = new XQuery(xq5);
  try (final PrintStream out = new PrintStream(new FileOutputStream(
		new File(new StringBuilder(LOCATION.getAbsolutePath())
				.append(File.separator).append(File.separator)
				.append("output-revision-1.xml").toString())))) {
    q.setPrettyPrint(true).serialize(ctx5, out);
  }
  System.out.println();
}

Builtin XQuery functions

XQuery functions in the default Brackit namespace.

Functions to store a document in Sirix.

bit:load(xs:string, xs:string+)

Example: bit:load('mydoc.xml', '%s')", doc)

Load a file into Sirix.

** bit:serialize(xs:item()*) as xs:string**

Example: bit:serialize(doc('mydoc.xml', 1))

Serialize a version of a document (the first version).

XQuery functions in the Sirix namespace.

Functions for I/O.

sdb:load($coll as xs:string, $res as xs:string, $fragment as xs:string, $create-new as xs:boolean?) as ()

Example: sdb:load('mydocs.col', 'resource1', '%s')", FileURL)

Loads a document into the mydocs.col collection/database in the "resource1" document.

sdb:store($coll as xs:string, $res as xs:string, $fragment as xs:node, $create-new as xs:boolean?) as ()

TODO: Example

Stores a document in the mydocs.col collection/database in the "resource1" document.

sdb:serialize(xs:item()*, xs:boolean()+, xs:string()+)

Example, sdb:serialize((doc('mydocs.col', 1), doc('mydocs.col', 2)), fn:boolean(1), 'output-revision-2.xml')

Serialize revision 1 and 2 of the only document in the mydocs.col collection. Use pretty printing (second parameter true) and serialize it to the File denoted by the name 'output-revision-2.xml'.

Create indexes.

Name indexes:

sdb:create-name-index($doc as node(), $include as xs:QName) as xs:node*

sdb:create-name-index($doc as node()) as xs:node()

Example: sdb:create-name-index($doc, fn:QName((), 'src')

Creates a name index on all elements/attributes with the localName "src" in the default namespace.

Path indexes:

sdb:create-path-index($doc as xs:node, $paths as xs:string) as xs:node*

sdb:create-path-index($doc as xs:node) as xs:node

Example: TODO

Scan indexes.

** TODO: **

Example: scan-cas-index($doc, sdb:find-cas-index($doc, 'xs:string', '//@*'), 'bar', true(), 0, ())

Scans a content-and-structure (CAS) index TODO.

** TODO: **

scan-path-index($doc, 0, '//log/*')

Scans the path index with the ID 0 for all log-paths.

Temporal axis

Example

doc('mydoc.xml')/log/all-time::* List of temporal axis:

  • future::
  • future-or-self::
  • past::
  • past-or-self::
  • previous::
  • previous-or-self::
  • next::
  • next-or-self::
  • first::
  • last::
  • all-time::

You are also able to invert the axis with the "not"-function. For instance:

doc('mydoc.xml', 2)/log/[not(past::)]