XMLII - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

XMLII

Description:

Many more features, including. uniqueness and foreign key ... XQuery is derived from the Quilt query language, which itself borrows from SQL, XQL and XML-QL ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 27
Provided by: marily230
Learn more at: http://www.cse.unt.edu
Category:
Tags: xmlii | quilt

less

Transcript and Presenter's Notes

Title: XMLII


1
XMLII
  • XSchema
  • XQuery
  • Oracle XSU

2
XML Schema
  • XML Schema is a more sophisticated schema
    language which addresses the drawbacks of DTDs.
    Supports
  • Typing of values
  • E.g. integer, string, etc
  • Also, constraints on min/max values
  • User-defined, complex types
  • Many more features, including
  • uniqueness and foreign key constraints,
    inheritance
  • XML Schema is itself specified in XML syntax,
    unlike DTDs
  • More-standard representation, but verbose
  • XML Scheme is integrated with namespaces
  • BUT XML Schema is significantly more
    complicated than DTDs.

3
XML Schema Version of Bank DTD
  • ltxsschema xmlnsxshttp//www.w3.org/2001/XMLSche
    magt
  • ltxselement namebank typeBankType/gt
  • ltxselement nameaccountgtltxscomplexTypegt
    ltxssequencegt ltxselement
    nameaccount_number typexsstring/gt
    ltxselement namebranch_name
    typexsstring/gt ltxselement
    namebalance typexsdecimal/gt
    lt/xssquencegtlt/xscomplexTypegt
  • lt/xselementgt
  • .. definitions of customer and depositor .
  • ltxscomplexType nameBankTypegtltxssquencegt
  • ltxselement refaccount minOccurs0
    maxOccursunbounded/gt
  • ltxselement refcustomer minOccurs0
    maxOccursunbounded/gt
  • ltxselement refdepositor minOccurs0
    maxOccursunbounded/gt
  • lt/xssequencegt
  • lt/xscomplexTypegt
  • lt/xsschemagt

4
XML Schema Version of Bank DTD
  • Choice of xs was ours -- any other namespace
    prefix could be chosen
  • Element bank has type BankType, which is
    defined separately
  • xscomplexType is used later to create the named
    complex type BankType
  • Element account has its type defined in-line

5
More features of XML Schema
  • Attributes specified by xsattribute tag
  • ltxsattribute name account_number/gt
  • adding the attribute use required means value
    must be specified

6
Querying and Transforming XML Data
  • Translation of information from one XML schema to
    another
  • Querying on XML data
  • Above two are closely related, and handled by the
    same tools
  • Standard XML querying/translation languages
  • XPath
  • Simple language consisting of path expressions
  • XSLT
  • Simple language designed for translation from XML
    to XML and XML to HTML
  • XQuery
  • An XML query language with a rich set of features

7
XQuery
  • XQuery is a general purpose query language for
    XML data
  • Currently being standardized by the World Wide
    Web Consortium (W3C)
  • The textbook description is based on a January
    2005 draft of the standard. The final version
    may differ, but major features likely to stay
    unchanged.
  • XQuery is derived from the Quilt query language,
    which itself borrows from SQL, XQL and XML-QL
  • XQuery uses a for let where order
    by result syntax for ? SQL from
    where ? SQL where order by ? SQL order by
  • result ? SQL select let allows
    temporary variables, and has no equivalent in SQL

8
FLWOR Syntax in XQuery
  • For clause uses XPath expressions, and variable
    in for clause ranges over values in the set
    returned by XPath
  • Simple FLWOR expression in XQuery
  • find all accounts with balance gt 400, with each
    result enclosed in an ltaccount_numbergt ..
    lt/account_numbergt tag for x in
    /bank-1/customer/account let acctno
    x/account_number/text()
  • where x/balance gt 400 return
    ltaccount_numbergt acctno lt/account_numbergt
  • Items in the return clause are XML text unless
    enclosed in , in which case they are evaluated
  • Let clause not really needed in this query, and
    selection can be done In XPath. Query can be
    written as
  • for x in /bank-1/customer/accountbalancegt400
    return x/account_number

9
Joins
  • Joins are specified in a manner very similar to
    SQLfor 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 ltcust_acctgt c a lt/cust_acctgt
  • 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 ltcust_acctgt c a lt/cust_acctgt

10
Nested Queries
  • The following query converts data from the flat
    structure for bank information into the nested
    structure used in bank-1
  • ltbank-1gt
  • for c in /bank/customer
  • return
  • ltcustomergt
  • c/
  • for d in /bank/depositorcustomer_name
    c/customer_name,
  • a in /bank/accountaccount_numberd/a
    ccount_number
  • return a
  • lt/customergt
  • lt/bank-1gt
  • c/ denotes all the children of the node to
    which c is bound, without the enclosing
    top-level tag
  • c/text() gives text content of an element
    without any subelements / tags

11
Sorting in XQuery
  • The order by clause can be used at the end of any
    expression. E.g. to return customers sorted by
    name for c in /bank/customer order by
    c/customer_name return ltcustomergt c/
    lt/customergt
  • Use order by c/customer_name to sort in
    descending order
  • Can sort at multiple levels of nesting (sort by
    customer_name, and by account_number within each
    customer)
  • ltbank-1gt for c in /bank/customer
  • order by c/customer_name
  • return ltcustomergt c/
  • for d in /bank/depositorcustomer_name
    c/customer_name, a in
    /bank/accountaccount_numberd/account_number
  • order by a/account_number
  • return ltaccountgt a/ lt/accountgt
    lt/customergt
  • lt/bank-1gt

12
Oracle XML-SQL Utility
  • Transform data from o-r tables/views into XML
  • Extract relevant data from an XML document
  • Insert data into database tables
  • Using a canonical mapping
  • Available as
  • Java command-line front end
  • Java API
  • PL/SQL API

13
SQL-to-XML Mapping
  • For each SQL query Q, will get the XML file with
    the following DTD
  • lt!DOCTYPE ROWSET
  • lt!ELEMENT ROWSET (ROW)gt
  • lt!ELEMENT ROW (attribute-list returned by
    Q)gt
  • gt
  • If some attribute is of nested table type, then
    the element will have sub-element corresponding
    to the attributes of the nested table

14
Example of SQL-to-XML
  • We first create a table country
  • CREATE TABLE country
  • (name char(50),
  • region char(60),
  • area decimal(10),
  • population decimal(11),
  • gdp decimal(14),
  • primary key (name)
  • )

15
Example of SQL-to-XML
  • CREATE TYPE CountriesType AS OBJECT (
  • country_name char(50),
  • area decimal(10),
  • population decimal(11),
  • gdp decimal(14)
  • )
  • /
  • CREATE TYPE CountriesTableType AS TABLE OF
    CountriesType
  • /
  • CREATE TABLE Regions (
  • region_name char(60),
  • countries CountriesTableType,
  • area decimal(10),
  • population decimal(11),
  • gdp decimal(14)
  • )
  • NESTED TABLE Countries STORE AS CountriesTable

16
Example of SQL-to-XML
  • INSERT INTO Regions(region_name,area,population,gd
    p)
  • SELECT region, SUM(area), SUM(population),
    SUM(gdp)
  • FROM country
  • GROUP BY region
  • UPDATE Regions
  • SET countries
  • CAST(MULTISET(
  • SELECT
  • FROM country
  • WHERE region
    Regions.region_name)
  • AS CountriesTableType )

17
Example of SQL-to-XML
  • Root ROWSET element
  • Each row ROW element
  • Each simple attribute sub-element of ROW
    element
  • Each nested table attribute sub-element with
    sub-element
  • Each ROW element has a num attribute
  • Null values are left out

18
Java front-end utility - OracleXML
  • Export CLASSPATH/usr/local/oracle/software/jdbc/l
    ib/classes12.zip/usr/local/oracle/software/rdbms/
    jlib/xsu11.jar.
  • java OracleXML getXML \
  • -user username/passwd\
  • -conn jdbcoraclethin_at_erg.csci.unt.edu1521ERG
    \
  • select from country
  • OracleXML parameters
  • User name and password
  • JDBC connection string
  • SQL query
  • Extract data from the database to XML form

19
A Few More Options
  • java OracleXML getXML -user user/passwd'
  • -conn 'jdbcoraclethin_at_erg.csci.unt.edu1521
    ERG' \
  • -withDTD -rowsetTag 'countries' -rowTag
    'country' -rowIdAttr 'countryID' \ 'select name
    as "_at_countryname", gdp, area from country
  • -withDTD generate the DTD
  • -rowsetTag specify the rowsetTag name
  • -rowTag specifiy the rowTag name
  • -rowIdAttr specify the name of the row ID
    attribute of each row
  • name as _at_countryname rename the name
    sub-element tag to countryname tag

20
XSU Java API
  • Through Java API
  • Allow generation of XML data
  • Allow insertion of data from an XML document
  • Allow delete and updates based on XML document

21
API classes
  • Oracle.xml.sql.query.OracleXMLQuery (for query)
  • Create a JDBC Connection
  • Create an OracelXMLQUery instance
  • Set options in the OracleXMLQuery object
  • Obtain the results
  • Oracle.xml.sql.dml.OracleXMLSave (for update,
    insert, and delete)
  • Create a JDBC Connection
  • Creat and OracleXMLSave object
  • Set options in the OracleXMLSave ojbect
  • Invoke the proper method to insert, delete, or
    update

22
Extracting XML
  • import oracle.jdbc.driver.
  • import oracle.xml.sql.query.OracleXMLQuery
  • import java.lang.
  • import java.sql.
  • public class xsuGet
  • public static void main(String argv) throws
    SQLException
  • try
  • Class.forName ("oracle.jdbc.driver.OracleDri
    ver")
  • catch (ClassNotFoundException e)
  • System.out.println ("Could not load the
    driver")
  • return
  • Connection conn
  • DriverManager.getConnection(
  • "jdbcoraclethin_at_erg.csci.unt.edu1521
    ERG", username",passwd")
  • // Create the query class.
  • OracleXMLQuery qry new OracleXMLQuery(conn,
    "select from regions")

23
XMLType
  • A system defined object
  • With built in member functions
  • Create, extract, and index XML data
  • Can be used as column type

24
Define a table with XMLType
  • CREATE TABLE addrbook(
  • name varchar(20),
  • card SYS.XMLTYPE,
  • creationDate Date
  • )
  • Address book has a name, a creationData, and a
    XMLType attribute to contain the contact
    information structured in XML format

25
XMLType Insertion
  • Use member function createXML(xml data)
    Example
  • insert into addrbook values
  • ('Roger', sys.XMLType.createXML(
  • 'ltACARD CREATEDBY"raj"gt
  • ltEMAILgtroger12_at_yahoo.comlt/EMAILgt
  • ltWPHONEgt111-5678lt/WPHONEgt
  • ltADDRESSgt
  • ltLINE1gt123 Main
    Streetlt/LINE1gt
  • ltCITYgtAtlantalt/CITYgt
  • ltSTATEgtGAlt/STATEgt
  • ltZIPgt33333lt/ZIPgt
  • lt/ADDRESSgt
  • lt/ACARDgt'), sysdate)

Non-well-formed XML will be rejected
26
Querying XML Data
  • Use member function
  • getClobVal CLOB (Character Large Object)
  • getStringVal
  • GetNumberVal
  • Extract(Xpath expression)
  • Example
  • select a.card.extract('/ACARD').getstringVal()
  • from addrbook a
  • where name 'Roger'
Write a Comment
User Comments (0)
About PowerShow.com