18

Oracle XML Handling

Embed Size (px)

Citation preview

Page 1: Oracle XML Handling
Page 2: Oracle XML Handling

XML Storage Options:1. VARCHAR2 – unstructured, limited size2. CLOBs – unstructured, max file = 2GB3. XMLType – structured, associate with XDK and other XML

operations

XML DB Architecture:1. XML DB Repository2. DOM fidelity3. SQL* Loader

Page 3: Oracle XML Handling

Using XMLTYPE

XML Piecewise UpdateUpdate part of the xml document in the database specified by the XPath expression.

XML Schema ValidationManually or automatically validate XML documents as they are inserted to the Database.

XML Document GenerationGenerate XML data from database using normal SQL query.

Page 4: Oracle XML Handling

Creating XMLType Column or table with optional XML Schema support

create table profile(pid number,pfile XMLType);

create table profile of XMLType;

create table profile of XMLTypeXMLSCHEMA “http://bumbus.ucdavis.edu/scholar.xsd”ELEMENT “UCLEADS”

Declares XMLType Column

Declares XMLType Table

Not Recommended

Declares XMLType Table conformed to an XML Schema and specific the root element of the xml document to be inserted.

Page 5: Oracle XML Handling
Page 6: Oracle XML Handling

Storing XML document into the databaseinsert into profilevalues(100, XMLType('

<ScholarProfile><ID>1</ID><LastName> Azzzr</LastName><FirstName>Hussain</FirstName><Email>[email protected]</Email><Major>Load Runner</Major><Grade>A</Grade>

</ScholarProfile>‘ ));

Insert the whole XML document in SQL query

Page 7: Oracle XML Handling

Accessing XML data stored as XMLType instance

1. ExtractValue()- access the value of an XML node

2. ExistsNode() - check if a particular node existed

3. Exact() - extract a collection of XML nodes

4. XMLSequence()- converts a fragment of XML into a collection (a table) of XMLType instances.

Page 8: Oracle XML Handling

SELECT extract(X.pfile,'UCLEADS/ScholarProfile/Major')FROM profile X;

Results are returned as a fragment of XML

Page 9: Oracle XML Handling
Page 10: Oracle XML Handling

SELECT extractValue(value(w),'/Major') FROM profile X, TABLE ( xmlsequence (

extract(value(X), '/UCLEADS/ScholarProfile/Major'))) w;

Values are extracted from the nodes of the XMLType table generated using the XMLSequence()

Page 11: Oracle XML Handling

SELECT existsNode(x.pfile, '/UCLEADS/ScholarProfile/Major') SubFound, existsNode(x.pfile, '/UCLEADS/ScholarProfile[Major="ORACLE"]') MajorFound, existsNode(x.pfile, '/UCLEADS/ScholarProfile[Major="Oracle"]') MajorFound2 FROM Profile1 X;

SELECT count(*) FROM Profile X WHERE existsNode(x.pfile, '/UCLEADS/ScholarProfile[Major="Oracle"‘]) = 1;

Page 12: Oracle XML Handling

<EMPLOYEES><EMP>

<EMPNO>112</EMPNO><EMPNAME>Joe</EMPNAME><SALARY>50000</SALARY>

</EMP><EMP>

<EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME><SALARY>60000</SALARY>

</EMP><EMP>

<EMPNO>412</EMPNO><EMPNAME>Jack</EMPNAME><SALARY>40000</SALARY>

</EMP></EMPLOYEES>

Page 13: Oracle XML Handling

SELECT UPDATEXML(emp_col, '/EMPLOYEES/EMP[EMPNAME="Joe"]/SALARY/text()', 100000,

'//EMP[EMPNAME="Jack"]/EMPNAME/text()','Jackson',

'//EMP[EMPNO=217]', XMLTYPE.CREATEXML('<EMP><EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME>'))

FROM emp_tab e;

UPDATEXML takes as arguments an XMLType instance and an XPath-value pair and returns an XMLType instance with the updated value

Page 14: Oracle XML Handling

<EMPLOYEES><EMP><EMPNO>112</EMPNO><EMPNAME>Joe</EMPNAME><SALARY>100000</SALARY></EMP><EMP><EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME></EMP><EMP><EMPNO>412</EMPNO><EMPNAME>Jackson</EMPNAME><SALARY>40000</SALARY></EMP></EMPLOYEES>

<EMPLOYEES><EMP>

<EMPNO>112</EMPNO><EMPNAME>Joe</EMPNAME><SALARY>50000</SALARY>

</EMP><EMP>

<EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME><SALARY>60000</SALARY>

</EMP><EMP>

<EMPNO>412</EMPNO><EMPNAME>Jack</EMPNAME><SALARY>40000</SALARY>

</EMP></EMPLOYEES>

Page 15: Oracle XML Handling

CREATE VIEW new_emp_viewAS SELECT

UPDATEXML(emp_col, '/EMPLOYEES/EMP/SALARY/text()', 0) emp_view_col

FROM emp_tab e

Page 16: Oracle XML Handling

UPDATE profile t SET value(t) = updateXML(value(t),'/UCLEADS/ScholarProfile/Major/text()','CS') WHERE existsNode(value(t),

'/UCLEADS/ScholarProfile[Major="Computer Science"]') = 1;

XML Piecewise Update

• isFragment() – returns (1) if the XMLType contains XML document fragment.

• getClobVal() – converts the XMLType document into CLOB object.

• getRootElement() – get the root element of the XML document.

• getNameSpace() – get the namespace of the root element of the XML document.

Page 17: Oracle XML Handling

select xmltype(xmltype.getclobVal(t.pfile)).getRootElement() Exmpl1, xmltype.getRootElement(t.pfile)Exmp2, xmltype.isFragment(t.pfile)isFrag1 , xmltype.isFragment(extract(t.pfile,'/UCLEADS/ScholarProfile'))isFrag2 from profile1 t;