6.2.7.Query Tools

Relational Data using SQL

Conductor Interactive SQL allows you to quickly and directly query Virtuoso using SQL. It offers Save and Load facilities which allow SQL queries to be saved as an XML template, and read back later. With a query in the SQL Statement box click on the Execute for the results which will in the "Base" tab with option to return back to the query area. The Clear clears the SQL Statement text box.

Figure6.98.Querying Relational Database Using SQL

Querying Relational Database Using SQL



Specify the location for the file to be saved to by selecting the "WebDAV source" or "Local file" check-box.

Figure6.100.Saving SQL in an XML Template

Saving SQL in an XML Template

Click the "Browse" button. As result will be opened the Virtuoso WebDAV/File Browser where you should define the XML template based on the SQL Query. Specify a Root Element that will contain the resulting XML tree. Specify the file name and location of the XML Template.

XML Data Using XQuery

The Conductor Interactive XQuery facility allows you to create, execute, save and reload queries using the evolving W3C XML Query (XQuery) Language . Virtuoso currently supports the 1.0 version of this language.

This language uses XPath-like expressions, as well as a set of functions and operators, to permit effective parallel searching of a set of XML documents. Where XPath works with one XML "tree", XQuery searches a "forest". The result is an XML document.

In order to create an XQuery you must both create the query statement - by typing or pasting it into the text box - and specify the document context. Since Virtuoso's XQuery implementation operates over XML data in relational tables, this means the tables and columns that are to be searched.

Note that the XQuery language also allows a query to specify all or part of the document context for the query. In the example below we will see how these can interact. The user interface form permits you to select a table - either one of the XQuery test data tables that come with Virtuoso, or the WS.WS.SYS_DAV_RES table, which stores Virtuoso's WebDAV Repository content.

The form specifies a Key Column and a Data Column . For the sample tables, the values for these are filled in for you. The Path is prepended to any document() function specified in the query text to find Key column values of XML trees against which the query is to be run.

Once a query has been written and debugged, it can be saved by pressing the Save button. This brings you to the form for saving a query as an XML Template in the DAV repository.

Pressing the Execute button causes the query result (an XML tree) to be shown on the page below the Statement type-in box.

Example6.16.XQuery Test File Example

In this example, we will query the table XQuery test files table, with "name" as the key column and "text" as the data column.

The query text, shown below, is a sample query from the W3C's XML Query Use Cases document (http://www.w3.org/TR/xmlquery-use-cases ). This query contains a document() call specifying a document named "bib.xml". In order to have the query run properly, we first set the Path form value to "xqdemo/". This causes the query to find all rows in the table XQ.XQ.TEST_FILES that have the value "xqdemo/bib.xml" in their Name column.

   for $b in document("bib.xml")/bib/book
   where $b/publisher = "Addison-Wesley" and $b/@year > 1991
      <book year = {$b/@year}>

Figure6.101.XQuery Test File Results

XQuery Test File Results