Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML and ORACLE Rosa Isela López Aguilar Noviembre 2008.

Similar presentations


Presentation on theme: "XML and ORACLE Rosa Isela López Aguilar Noviembre 2008."— Presentation transcript:

1 XML and ORACLE Rosa Isela López Aguilar Noviembre 2008

2 Oracle XML DB Oracle Database supports native XML generation. Oracle provides you with several options for generating or regenerating XML data when stored in: Oracle Database, in general Oracle Database in XMLTypes columns and tables

3 Creating a Table with an XMLType Column CREATE TABLE Ejemplo1(KEYVALUE varchar2(10) primary key, XMLCOLUMN xmltype); select * from Ejemplo1; Creating a Table of XMLType CREATE TABLE XMLTABLE OF XMLType; select * from XMLTABLE;

4 INSERT INTO ejemplo1 VALUES(100, XMLType(' Owned ')); select * from ejemplo1; Inserting Values on Ejemplo1

5 INSERT INTO ejemplo1 VALUES(101, XMLType('<PurchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.oracle.com/xdb/po.xsd"> ADAMS-20011127121040988PST SCOTT 2002-03-31 Julie P. Adams ADAMS R20 Julie P. Adams Redwood Shores, CA 94065 650 506 7300 Ground The Ruling Class Diabolique 8 1/2 ')); select * from ejemplo1; Inserting Values on Ejemplo1

6 existsNode() Examples That Find a Node to Match the XPath Expression Given this sample XML document, the following existsNode() operators return true (1). SELECT existsNode(XMLCOLUMN,'/PurchaseOrder/Reference')‏ FROM ejemplo1; SELECT existsNode(XMLCOLUMN,'/PurchaseOrder[Reference="ADAMS- 20011127121040988PST"]')‏ FROM ejemplo1; SELECT existsNode(XMLCOLUMN, '/PurchaseOrder/LineItems/LineItem[2]/Part[@Id="037429135020"]')‏ FROM ejemplo1; SELECT existsNode(XMLCOLUMN, '/PurchaseOrder/LineItems/LineItem[Description="8 1/2"]')‏ FROM ejemplo1;

7 Overview of Generating XML Using Standard SQL/XML Functions You can generate XML data using any of the following standard SQL/XML functions supported by Oracle XML DB: XMLELEMENT and XMLATTRIBUTES SQL Functions XMLFOREST SQL Function XMLCONCAT SQL Function XMLAGG SQL Function XMLPI SQL Function XMLCOMMENT SQL Function XMLROOT SQL Function XMLSERIALIZE SQL Function XMLPARSE SQL Function

8 SELECT XMLCOLUMN, XMLELEMENT("emp", KEYVALUE) AS "result" FROM ejemplo1 WHERE KEYVALUE > 100; XMLELEMENT XMLElement(): Generating Nested XML SELECT XMLELEMENT("Emp", XMLELEMENT("name", XMLCOLUMN), XMLELEMENT ( "emp", KEYVALUE)) AS "result" FROM ejemplo1 WHERE KEYVALUE > 100 ; XMLElement(): Generating an Element for Each Employee with ID Attribute SELECT XMLELEMENT("Emp", XMLATTRIBUTES(KEYVALUE AS "ID") ) AS "result" FROM ejemplo1 WHERE KEYVALUE > 100;

9 SELECT extract(XMLCOLUMN,'/emp//enumber') FROM ejemplo1; XML EXTRACT Using existsNode() in the WHERE Clause SELECT count(*) FROM ejemplo1 WHERE existsNode(XMLCOLUMN,'/PurchaseOrder[User="ADAM S"]') = 1; DELETE FROM ejemplo1 WHERE existsNode(XMLCOLUMN,'/PurchaseOrder[User="ADAM S"]') = 1; Using delete in the WHERE Clause

10 XMLELEMENT: Generating an Element for Each Employee This example produces an Emp element for each employee, with the employee name as its content: SELECT e.employee_id, XMLELEMENT ("Emp", e.first_name ||' '|| e.last_name) AS "RESULT" FROM hr.employees e WHERE employee_id > 200; This query produces the following typical result: EMPLOYEE_ID RESULT ----------- ----------------------------------- 201 Michael Hartstein 202 Pat Fay 203 Susan Mavris 204 Hermann Baer 205 Shelley Higgins 206 William Gietz

11 SELECT KEYVALUE, XMLELEMENT("NUEVO", KEYVALUE||' '||XMLCOLUMN) AS "RESULT" FROM ejemplo2 WHERE KEYVALUE=101; XMLELEMENT: Generating an Element for a Particular Case

12 XMLELEMENT: Generating Nested XML To produce an Emp element for each employee, with elements that provide the employee name and hire date, do the following: SELECT XMLElement("Emp", XMLElement("name", e.first_name ||' '|| e.last_name), XMLElement("hiredate", e.hire_date)) AS "RESULT" FROM hr.employees e WHERE employee_id > 200 ; RESULT ----------------------------------------------------------------------- Michael Hartstein 1996-02- 17 Pat Fay 1997-08- 17 Susan Mavris 1994-06- 07 Hermann Baer 1994-06- 07

13 XMLELEMENT: Generating Employee Elements with ID and Name Attributes This example produces an Emp element for each employee, with an id and name attribute: SELECT XMLElement("Emp", XMLAttributes( e.employee_id as "ID", e.first_name ||' ' || e.last_name AS "name"))‏ AS "RESULT" FROM hr.employees e WHERE employee_id > 200; RESULT -----------------------------------------------

14 XMLELEMENT: Generating an Element from a User-Defined Datatype Instance CREATE OR REPLACE TYPE emp_t AS OBJECT ("@EMPNO" NUMBER(4), ENAME VARCHAR2(10)); CREATE OR REPLACE TYPE emplist_t AS TABLE OF emp_t; CREATE OR REPLACE TYPE dept_t AS OBJECT ("@DEPTNO" NUMBER(2), DNAME VARCHAR2(14), EMP_LIST emplist_t); SELECT XMLElement("Department", dept_t(department_id, department_name, CAST(MULTISET(SELECT employee_id, last_name FROM hr.employees e WHERE e.department_id = d.department_id)‏ AS emplist_t)))‏ AS deptxml FROM hr.departments d WHERE d.department_id = 10;

15 This produces an XML document which contains the Department element and the canonical mapping of type dept_t. DEPTXML ------------- ACCOUNTING CLARK KING MILLER

16 Accessing a Text Node Value Matching an XPath Expression Using extractValue()‏ SELECT extractValue(XMLCOLUMN,'/PurchaseOrder/Reference') FROM ejemplo1; SELECT extractValue(XMLCOLUMN,'/Description')‏ FROM ejemplo1, TABLE ( xmlsequence (extract(XMLCOLUMN,'/PurchaseOrder/LineItems/LineItem/Descripti on')‏ )‏ ) t;

17 Using updateXML() to Replace Contents of a Node Tree Associated with XPath Elements In this example updateXML() replaces the contents of the node tree associated with the element identified by the XPath expression `/PurchaseOrders/LineItems/LineItem[2]'. UPDATE ejemplo1 SET xmlcolumn = updateXML(xmlcolumn, '/PurchaseOrder/LineItems/LineItem[2]', xmltype(' Andrei Rublev <Part Id="715515009928" UnitPrice="39.95" Quantity="2"/> ' )‏ ) WHERE existsNode(XMLCOLUMN, '/PurchaseOrder[Reference="MILLER- 200203311200000000PST"]' ) = 1; SELECT * FROM ejemplo1;

18 Using updateXML() to Update a Text Node Value Identified by an XPath Expression This example uses updateXML() to update the value of the text node identified by the XPath expression `/PurchaseOrder/Reference': UPDATE ejemplo1 SET XMLCOLUMN = updateXML(XMLCOLUMN,'/PurchaseOrder/Reference/text()', 'MILLER-200203311200000000PST')‏ WHERE existsNode(XMLCOLUMN,'/PurchaseOrder[Reference="ADAMS- 20011127121040988PST"]') = 1; SELECT * FROM ejemplo1;

19 XMLFOREST: Generating Elements with Attribute and Child Elements This example generates an Emp element for each employee, with a name attribute and elements with the employee hire date and department as the content. SELECT XMLElement("Emp", XMLAttributes(e.first_name ||' '|| e.last_name AS "name"), XMLForest(e.hire_date, e.department AS "department"))‏ AS "RESULT" FROM employees e WHERE e.department_id = 20; RESULT ------------------------------------- 1996-02-17 20 1997-08-17 20

20 XMLFOREST: Generating an Element from a User-Defined Datatype Instance SELECT XMLForest( dept_t(department_id, department_name, CAST (MULTISET (SELECT employee_id, last_name FROM hr.employees e WHERE e.department_id = d.department_id)‏ AS emplist_t))‏ AS "Department")‏ AS deptxml FROM hr.departments d WHERE department_id=10; DEPTXML --------------------------------- Administration Whalen

21 SELECT XMLELEMENT("Emp", XMLFOREST(KEYVALUE, XMLCOLUMN))‏ "Emp Element" FROM ejemplo1 WHERE KEYVALUE > 99; XMLFOREST

22 CREATE TABLE datosxml(Colxml XMLType); CREATE TABLE xsl_tab (col1 XMLTYPE); Insert Into datosxml Values ( xmltype(' Juan garcia ')); SELECT * FROM datosxml; XMLTRANSFORM

23 Inserting StyleSheet: INSERT INTO xsl_tab VALUES (XMLTYPE.createxml(' <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:sort select="name(.)" data-type="text" order="ascending"/> ')); XMLTRANSFORM

24 SELECT XMLTRANSFORM(d.colxml, x.col1) FROM datosxml d, xsl_tab x; Result XMLTRANSFORM

25 http://www.acs.ilstu.edu/docs/oracle/server.101/b10759/functi ons204.htm http://lbd.epfl.ch/f/teaching/courses/oracle9i/appdev.920/a966 20/xdb03usg.htm#1656 Referencias


Download ppt "XML and ORACLE Rosa Isela López Aguilar Noviembre 2008."

Similar presentations


Ads by Google