XML and ORACLE Rosa Isela López Aguilar Noviembre 2008
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
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;
INSERT INTO ejemplo1 VALUES(100, XMLType(' Owned ')); select * from ejemplo1; Inserting Values on Ejemplo1
INSERT INTO ejemplo1 VALUES(101, XMLType('<PurchaseOrder xmlns:xsi=" xsi:noNamespaceSchemaLocation=" ADAMS PST SCOTT Julie P. Adams ADAMS R20 Julie P. Adams Redwood Shores, CA Ground The Ruling Class Diabolique 8 1/2 ')); select * from ejemplo1; Inserting Values on Ejemplo1
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 PST"]') FROM ejemplo1; SELECT existsNode(XMLCOLUMN, FROM ejemplo1; SELECT existsNode(XMLCOLUMN, '/PurchaseOrder/LineItems/LineItem[Description="8 1/2"]') FROM ejemplo1;
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
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;
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
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 Michael Hartstein 202 Pat Fay 203 Susan Mavris 204 Hermann Baer 205 Shelley Higgins 206 William Gietz
SELECT KEYVALUE, XMLELEMENT("NUEVO", KEYVALUE||' '||XMLCOLUMN) AS "RESULT" FROM ejemplo2 WHERE KEYVALUE=101; XMLELEMENT: Generating an Element for a Particular Case
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 Pat Fay Susan Mavris Hermann Baer
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
XMLELEMENT: Generating an Element from a User-Defined Datatype Instance CREATE OR REPLACE TYPE emp_t AS OBJECT NUMBER(4), ENAME VARCHAR2(10)); CREATE OR REPLACE TYPE emplist_t AS TABLE OF emp_t; CREATE OR REPLACE TYPE dept_t AS OBJECT 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;
This produces an XML document which contains the Department element and the canonical mapping of type dept_t. DEPTXML ACCOUNTING CLARK KING MILLER
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;
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=" " UnitPrice="39.95" Quantity="2"/> ' ) ) WHERE existsNode(XMLCOLUMN, '/PurchaseOrder[Reference="MILLER PST"]' ) = 1; SELECT * FROM ejemplo1;
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 PST') WHERE existsNode(XMLCOLUMN,'/PurchaseOrder[Reference="ADAMS PST"]') = 1; SELECT * FROM ejemplo1;
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
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
SELECT XMLELEMENT("Emp", XMLFOREST(KEYVALUE, XMLCOLUMN)) "Emp Element" FROM ejemplo1 WHERE KEYVALUE > 99; XMLFOREST
CREATE TABLE datosxml(Colxml XMLType); CREATE TABLE xsl_tab (col1 XMLTYPE); Insert Into datosxml Values ( xmltype(' Juan garcia ')); SELECT * FROM datosxml; XMLTRANSFORM
Inserting StyleSheet: INSERT INTO xsl_tab VALUES (XMLTYPE.createxml(' <xsl:stylesheet version="1.0" xmlns:xsl=" > <xsl:sort select="name(.)" data-type="text" order="ascending"/> ')); XMLTRANSFORM
SELECT XMLTRANSFORM(d.colxml, x.col1) FROM datosxml d, xsl_tab x; Result XMLTRANSFORM
ons204.htm 20/xdb03usg.htm#1656 Referencias