Siam6 PDF
Siam6 PDF
XSLT XSL-FO
</procedure> E A unit="sec" E E
warning
T T T E T
15 Grip bulb. Rotate it counterclockwise.
one possible T
instance of the slowly
■ Limitations:
■ Operates on one document (no joins)
■ No grouping or aggregation
■ No facility for generating new output structures
XQuery
■ XQuery is a general purpose query language for XML data
■ Standardized by the World Wide Web Consortium (W3C)
■ XQuery is derived from
■ the Quilt (“Quilt” refers both to the origin of the language and to its use in “knitting ” together heterogeneous
data sources) query language, which itself borrows from
FOR_clause RETURN_clause
■ FOR clause, LET clause generate list of tuples of bound variables (order preserving) by
■ iterating over a set of nodes (possibly specified by an XPath expression), or
■ binding a variable to the result of an expression
■ WHERE clause applies a predicate to filter the tuples produced by FOR/LET
■ ORDER BY clause imposes order on the surviving tuples
■ RETURN clause is executed for each surviving tuple, generates ordered list of outputs
■ Associations to SQL query expressions
for ⬄ SQL from
where ⬄ SQL where
order by ⬄ SQL order by
return ⬄ SQL select
let allows temporary variables, and has no equivalent in SQL
Evaluating FLWOR Expressions
input sequence
tuple stream
$x $y $z
$x $y $z
… ok!
FOR $X,$Y ..
LET $Z .. WHERE ..
ok!
… …
X …
… … …
ORDER
BY ..
ouput sequence $x $y $z
…
RETURN ..
… …
…
FLWOR - Examples
■ Simple FLWR expression in XQuery
■ Find all accounts with balance > 400, with each result enclosed in an <account-
number> .. </account-number> tag
for $x in /bank-2/account
let $acctno := $x/@account-number
where $x/balance > 400
return <account-number> {$acctno} </account-number>
■ Let and Where clause not really needed in this query, and selection can be
done in XPath.
■ Query can be written as:
for $x in /bank-2/account[balance>400] return
<account-number> {$x/@account-number}
</account-number>
Nesting of Expressions
■ Here: nesting inside the return clause
■ Example: inversion of a hierarchy
<book> <author>
<title> <name>
<author> <title>
<author> FOR $a IN fn:distinct-values(//author) <title>
</book> ORDER BY $a/name </author>
<book> RETURN <author>
<title> <author> <name>
<author> <name> { $a/text() } </name> <title>
<author> { FOR $b IN //book[author = $a] <title>
</book> RETURN $b/title } </author>
</author>
XQuery: Joins
■ Joins are specified in a manner very similar to SQL
for $a in /bank/account,
$c in /bank/customer,
$d in /bank/depositor
where $a/account-number = $d/account-number
and $c/customer-name = $d/customer-name
return <cust-acct>{ $c $a }</cust-acct>
■ The same query can be expressed with the selections specified as XPath
selections:
for $a in /bank/account
$c in /bank/customer
$d in /bank/depositor[
account-number =$a/account-number and
customer-name = $c/customer-name]
return <cust-acct>{ $c $a }</cust-acct>
XQJ – Main Concepts
■ Similar to JDBC, but for XQuery statements
■ data source, connection, (prepared) XQuery expression (statement)
■ XQuery variable identifier instead of parameter markers ("?")
■ Query result is a sequence (XQSequence)
■ iterate through sequence items using XQSequence.next()
■ retrieve Java DOM objects using XQSequence.getObject()
■ retrieve atomic values as character string or mapped to Java data types
■ individual items or the complete stream can be "written" to the SAX API
■ Support for "serializing" an XQuery result
■ to file, Java writer, string
■ as (X)HTML
XQuery - Status
■ XQuery 1.0 is a w3c recommendation since January 2007
■ XQuery API for JavaTM (XQJ) is final (JSR) since 2009
■ XQuery Update Facility 1.0 is a candidate recommendation
■ XQuery 1.1 is in the making (working draft), work items include
■ value-based and positional grouping
■ outer join support
■ windowing
■ date and numeric value formatting
■ Additional ongoing work
■ XQuery and XPath Full Text 1.0 (candidate recommendation)
■ adds support for text retrieval in XQuery
■ XQuery Scripting Extensions 1.0 (working draft)
■ adds procedural features
Transforming XML Data: XSLT
■ A stylesheet stores formatting options for a document, usually separately
from document
■ E.g. HTML style sheet may specify font colors and sizes for headings, etc.
■ The XML Stylesheet Language (XSL) was originally designed for
generating HTML from XML
■ XSLT is a general-purpose transformation language
■ Can translate XML to XML, and XML to HTML
■ XSLT transformations are expressed using rules called templates
■ Templates combine selection using XPath with construction of results
Understanding A Template
■ Most templates have the following form:
<xsl:template match="emphasis">
<i><xsl:apply-templates/></i>
</xsl:template>
■ The whole <xsl:template> element is a template
■ The match pattern determines where this template applies
■ XPath pattern
■ Literal result element(s) come from non-XSL namespace(s)
■ XSLT elements come from the XSL namespace
SQL and XML
■ Use existing (object-)relational technology?
■ Large Objects: granularity understood by DBMS may be too coarse!
■ search/retrieval of subsets, update of documents
■ Decompose into tables: often complex, inefficient
■ mapping complexity, especially for highly "denormalized" documents
■ Useful, but not sufficient
■ should be standardized as part of SQL
■ but needs further enhancement to support "native" XML support in SQL
■ Enable "hybrid" XML/relational data management
■ supports both relational and XML data
■ storage, access
■ query language
■ programming interfaces
■ ability to view/access relational as XML, and XML as relational
■ all major relational DBMS vendors are moving into this direction
SQL/XML Big Picture
XML, enhanced
SQL client
XQuery client SQL client
SQL/XML
storage
<?xml version = "1.0"?>
<order>
<item> … </item> <?xml version = "1.0"?>
<order>
<item> … </item> <item> … </item>
… <item> … </item>
</order> …
</order>
SQL: Parts and Packages
•Two major goals:
•"Publish" SQL query results as XML documents 14: XML
•Ability to store and retrieve XML documents
• Rules for mapping SQL types, SQL identifiers and
SQL data values to and from corresponding
XML concepts
•A new built-in type XML
• A number of built-in operators that produce
values of type XML
dept_doc ==>
<name>Yates</name>
<name>Smith</name
<name>Martin</name>
JDBC-Support for SQLXML
■ New methods to create and retrieve SQLXML
■ Connection.createSQLXML()
■ ResultSet.getSQLXML()
■ PreparedStatement.setSQLXML()
■ SQLXML interface supports methods for accessing its XML content
■ getString()
■ getBinaryStream(), get CharacterStream()
■ obtain a Java stream/reader that can be passed directly to an XML parser
■ getSource()
■ obtain a source object suitable for XML parsers and XSLT transformers
■ corresponding setXXX() methods to initialize newly created SQLXML objects
QUESTIONS?????