Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Oracle XML DB and Native Web Services
Ondrej Kupka
December 3, 2012
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Section Layout
1 Oracle XML DBOverviewCore Ideas and ArchitectureStoring XML DataExampleStructured Model and XML SchemasXML/SQL Duality
2 Oracle XML DB Repository
3 Oracle XML DB Native Web Services
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Overview
Overview and Architecture
Oracle XML DB is a set of technologies for handling of XML data:
Storing
Generating
Accessing
Searching
Validating
Transforming
Indexing
The goal is to provide both XML and SQL access to the data(XML/SQL duality). XML data can be accessed using SQL, XMLcan be generated from relational data.
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Core Ideas and Architecture
Challenges and Ideas
We have a RDBMS and we have XML data. That is a completelydifferent thing. Relations and hierarchy is like apples and oranges.But we still have this RDBMS, so we should use it as much aspossible.
But we cannot say anything about a XML document’s structure.Unless we know the schema, that is!
Oracle XML DB: Abstract XMLType database object type, whichbasically behaves differently according to how much it knows aboutthe data. The access is the same, but the storage is optimisedwhen the structure is known.
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Storing XML Data
Storing XML DataXMLType
XMLType is an abstract data type for native handling of XML datain the database.
It is a database object type - tables of XMLType.
It can be used as a column type.
It can be passed into and out of PL/SQL procedures.
It can use various storage models according to the situation.
. . .
Native XML operations (XMLType methods):
extract() - extract a subset of nodes
existsNode() - check if particular node exists in a XMLTypeinstance
schemaValidate() - validate the content of a XMLTypeinstance against a schema
transform() - XSLT
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Storing XML Data
XMLType Storage Model
XMType tables and columns may be stored in these ways:
1 Structured storage (also object-relational, object-basedpersistence)
2 Unstructured storage (also CLOB, text-based persistence)
3 Binary XML (also post-parse persistence)
4 Hybrid of structured and unstructured
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Storing XML Data
Intermezzo - Data-Centric vs. Document-CentricThe Interesting Part
What are those various backend models good for? Well, it dependson how you approach your data and how you use it!
Data-Centric
XML schema-based dataLittle variation and little change over timeLeads to Object-Relational approach, B-treeindices . . .
Document-Centric
Variable, free-form dataLeads to CLOB or Binary XML, XMLIndexindices . . .
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Storing XML Data
Storage Models in Details - Structured Storage
Pros
Queries fast - relational query performanceUpdates fast - only the part affectedSpace efficiencyB-tree index
Cons
ThroughputData flexibility - schema conformanceSchema flexibility - one XMLType = singleschema
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Storing XML Data
Storage Models in Details - XML Binary Storage
Pros
Throughput - just encode/decodeQueries - streaming XPath eval, multiple atonce, fast access using XMLIntexSpace efficiencyFlexibilityFull schema validation
Cons
Not really any significant disadvantage
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Storing XML Data
Storage Models in Details - Unstructured Storage
Pros
ThroughputFlexibilityStreams-based replication
Cons
Queries slow - constructing DOM every timeUpdates slow - DOM, then the whole doc mustbe written backSpace inefficient
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Example
ExampleCreate and Insert
Insert using SQL, PL/SQL, Java, C . . .
Conventional and Direct-Path Load Mode
CREATE TABLE mytable2 OF XMLType;
CREATE DIRECTORY xmldir AS path_to_folder_containing_XML_file;
INSERT INTO mytable2 VALUES (XMLType(bfilename(’XMLDIR’, ’file.xml’),
nls_charset_id(’AL32UTF8’)));
DECLARE
res BOOLEAN;
BEGIN
res := DBMS_XDB.createResource(’/home/user/...’,
bfilename(’XMLDIR’, ’file.xml’),
nls_charset_id(’AL32UTF8’));
END;/
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Example
ExampleSelect
SELECT XMLCast(XMLQuery(’$p/<XPath>’ PASSING OBJECT_VALUE AS "p"
RETURNING CONTENT)
AS VARCHAR2(30)) "Reference"
FROM table
WHERE XMLExists(’$p/<XPath condition>’ PASSING OBJECT_VALUE AS "p");
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Structured Model and XML Schemas
Structured Mode and XML Schemas
Oracle decomposes XML into SQL objects according to the XMLschema.
Mapping of XML types into SQL types (complexType intoSQL type)
Element or attribute becomes a SQL attribute in that type
Collections as tables or LOBs or out-of-line storage (REFs)
Full utilisation or Oracle database
Additional constrains not possible in a plain XML schemaIt is possible to annotate the XML schema to define storage model,override types mapping and name database objects.
xdb:storeVarrayAsTable="true"
<xs:attribute name="ItemID" type="xs:integer"
xdb:SQLName="ITEMID"
xdb:SQLType="NUMBER"/>
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Structured Model and XML Schemas
XML Schemas Registration
You have to register your XML schemas before they can be used tovalidate objects. You need
1 The XML schema document
2 A string to be used as a identifier, typically an URL
BEGIN
DBMS_XMLSCHEMA.registerSchema(
SCHEMAURL => ’http://localhost:8080/src/schemas/xsd/file.xsd’,
SCHEMADOC => XDBURIType(’/src/schemas/xsd/file.xsd’).getCLOB(),
LOCAL => TRUE,
GENTYPES => TRUE,
GENTABLES => TRUE); # USUALLY YOU DON’T WANT THIS!!!
END;
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
XML/SQL Duality
XML/SQL Duality
The same data can be exposed as either rows in a table, beingmanipulated with SQL, or as nodes in a XML document andmanipulated using DOM, XSLT . . .
Oracle supports SQL/XML standard. This allows you address XMLcontent anywhere in your SQL. It uses XPath or XQuery to identifynodes to operate on.
XPath rewrite optimizes stuff.
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
XML/SQL Duality
XMLTable
Result of XMLQuery may include multiple elements without theroot node, so-called XML fragments. XMLTable can turn such afragment into multiple rows to perform joins and similar.
SELECT vt.COLUMN_VALUE
FROM table t,
XMLTable(’<XPath>’
PASSING t.OBJECT_VALUE) vt
WHERE XMLExists(’$t/<XPath>’
PASSING OBJECT_VALUE AS "t");
COLUMN_VALUE
------------
<e>...</e>
<e>...</e>
<e>...</e>
3 rows selected.
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
XML/SQL Duality
Relational Views
It is very simple, just create a view (CREATE OR REPLACEVIEW) using virtual tables!
Completely transparent to the view user
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
XML/SQL Duality
Generating XML Data from Relational Data
SQL/XML functions to construct XML
XMLElement
XMLAttributes
XMLForest
Use in the SELECT clause - SELECTXMLElement(XMLAttributes(...))
DBURIType
Simple way - convert table name into element, multiple¡ROW¿s containing columns as elements
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Section Layout
1 Oracle XML DBOverviewCore Ideas and ArchitectureStoring XML DataExampleStructured Model and XML SchemasXML/SQL Duality
2 Oracle XML DB Repository
3 Oracle XML DB Native Web Services
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Oracle XML DB Repository
It is an interface to your XML documents which imitates folder/filehierarchy. Data accessible using FTP, WebDAV, HTTP(S). It isbased on so-called resources. A resource is usually an XMLdocument. It has corresponding metadata file attached to it,holding ACL.
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Accessing Repository from SQL
RESOURCE VIEW
Columns: RES, ANY PATH, RESID
PATH VIEW
Columns: PATH, RES, LINK, RESID
SELECT path(1) path, depth(1) depth FROM PATH_VIEW
WHERE under_path(RES, 3, ’/sys’, 1) = 1;
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Section Layout
1 Oracle XML DBOverviewCore Ideas and ArchitectureStoring XML DataExampleStructured Model and XML SchemasXML/SQL Duality
2 Oracle XML DB Repository
3 Oracle XML DB Native Web Services
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Oracle XML DB Native Web Services
You can enable web access for your XML DB in form of servlets.Log in as SYS. Use either xdbconfig or following. User needsXDB WEBSERVICES. WSDL generated automatically.
DECLARE
SERVLET_NAME VARCHAR2(32) := ’...’;
BEGIN
DBMS_XDB.deleteServletMapping(SERVLET_NAME);
DBMS_XDB.deleteServlet(SERVLET_NAME);
DBMS_XDB.addServlet(NAME => SERVLET_NAME,
LANGUAGE => ’C’,
DISPNAME => ’Web Service’,
DESCRIPT => ’Servlet for queries over the web’,
SCHEMA => ’XDB’);
DBMS_XDB.addServletSecRole(SERVNAME => SERVLET_NAME,
ROLENAME => ’XDB_WEBSERVICES’,
ROLELINK => ’XDB_WEBSERVICES’);
DBMS_XDB.addServletMapping(PATTERN => ’/wsv/*’,
NAME => SERVLET_NAME);
END;
/
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Query Over The Web
<?xml version="1.0" ?>
<env:Envelope xmlns:env="http://www.w3.org/2002/06/soap-envelope ">
<env:Body>
<query xmlns="http://xmlns.oracle.com/orawsv">
<query_text type="SQL">
<![CDATA[SELECT * FROM employees WHERE salary = :e]]>
</query_text>
<bind name="e">50</bind>
<pretty_print>true</pretty_print>
</query>
</env:Body>
</env:Envelope>
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
PL/SQP Over The Web
You can use it, but it is a mess. You basically encode theparameters for the function into a SOAP request and do kind ofRPC, receiving the return value as XML again.
Oracle XML DB Oracle XML DB Repository Oracle XML DB Native Web Services
Thank you!