XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists...

26
XML datu izgūšana no XMLType tipa kolonām 1. SQL funkciju izmantošana: extract() atgriež elementu vai elementu kopu; existsNode() nosaka vai attiecīgs elements vai dokumenta daļa eksistē (vērtība =1); extractValue() atgriež elementa vērtību. 2. SQL/XML standarta funkcijas XMLQuery, XMLTable, XMLExists, and XMLCast. 3. SQL, PL/SQL vai Java valodu izmantošana. 4. XQuery valodas lietošana. 5. Oracle Text operatoru izmantošana XML dokumenta satura izgūšanai. XMLType metodes XML dokumenta izgūšanai dažādu datu tipu veidā: getCLOBVal(); getStringVal(); getNumberVal(); getBLOBVal(csid); 1

Transcript of XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists...

Page 1: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

XML datu izgūšana no XMLType tipa kolonām

1. SQL funkciju izmantošana:extract() atgriež elementu vai elementu kopu;existsNode() nosaka vai attiecīgs elements vai dokumenta daļa eksistē (vērtība =1);extractValue() atgriež elementa vērtību.

2. SQL/XML standarta funkcijas XMLQuery, XMLTable, XMLExists, and XMLCast.

3. SQL, PL/SQL vai Java valodu izmantošana.

4. XQuery valodas lietošana.

5. Oracle Text operatoru izmantošana XML dokumenta satura izgūšanai.

XMLType metodes XML dokumenta izgūšanai dažādu datu tipu veidā: getCLOBVal();getStringVal(); getNumberVal();getBLOBVal(csid);

1

Page 2: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

XML dokumenta shemas reģistrēšana

create or replace directory XMLDIR as 'G:/____GRAMATA/11_XML_DB/XML_piemeri';

beginDBMS_XMLSCHEMA.REGISTERSCHEMA(SCHEMAURL => 'http://localhost:8080/public/GRAMATVEIKALS.xsd',SCHEMADOC => bfilename('XMLDIR', 'GRAMATVEIKALS.xsd'),LOCAL => TRUE,CSID => nls_charset_id('AL32UTF8') ) ;end;

SCHEMAURL – adrese.SCHEMADOC – avots.CSID – simbolu kods.

XML shēmas nodzēšana

begin DBMS_XMLSCHEMA.DELETESCHEMA( SCHEMAURL => 'http://localhost:8080/public/GRAMATVEIKALS.xsd', DELETE_OPTION => DBMS_XMLSCHEMA.DELETE_CASCADE_FORCE);end;

2

Page 3: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

XMLType tipa tabulas definēšana, kurai norādīta XML shēma

create table GRAMATVEIKALS of XMLTypeXMLSCHEMA "http://xmlns.oracle.com/xdb/schemas/SYSTEM/localhost:8080/public/gramatveikals.xsd"ELEMENT "gramatveikals";

select ELEMENT_NAME, TABLE_NAME, STORAGE_TYPE from USER_XML_TABLESwhere XMLSCHEMA = 'http://localhost:8080/public/gramatveikals.xsd';

3

Page 4: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

XML dokumentu datu ievade teksta veidā

insert into GRAMATVEIKALS values (XMLType ('<?xml version="1.0" encoding="UTF-8"?><gramatveikals> <gramata kategorija="Pavārgrāmatas"> <virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts> <autors>Juris Kālis</autors> <gads>2005</gads> <cena>15.00</cena> </gramata> <gramata kategorija="Daiļliteratūra"> <virsraksts valoda="latviešu">Biezais sniegs</virsraksts> <autors>Anita Zemniece</autors> <gads>2008</gads> <cena>10.00</cena> </gramata></gramatveikals>'));

select Extract(object_VALUE, '//gramata')from GRAMATVEIKALS A;

<gramata kategorija="Pavārgrāmatas"><virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts><autors>Juris Kālis</autors><gads>2005</gads><cena>15.00</cena>

</gramata><gramata kategorija="Daiļliteratūra">

<virsraksts valoda="latviešu">Biezais sniegs</virsraksts><autors>Anita Zemniece</autors><gads>2008</gads><cena>10.00</cena>

</gramata>

4

Page 5: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

Retrieving the content of an XML document using pseudocolumn OBJECT_VALUE

Pseudocolumn OBJECT_VALUE can be used as an alias for the value of an object table. For an XMLType table that consists of a single column of XMLType, the entire XML document is retrieved. (OBJECT_VALUE replaces the value(x) and SYS_NC_ROWINFO$ aliases used in releases prior to Oracle Database10g Release 1.)

select OBJECT_VALUE from GRAMATVEIKALS;

5

Page 6: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

SQL funkcijas Extract() izmantošana, lai iegūtu mezglu (node) vērtības (values)

select Extract(OBJECT_VALUE, '/gramatveikals/gramata/virsraksts') "VIRSRAKSTI"from GRAMATVEIKALS;

<virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts><virsraksts valoda="latviešu">Biezais sniegs</virsraksts>

select Extract(OBJECT_VALUE, '/gramatveikals/gramata[@kategorija="Pavārgrāmatas"]')

<gramata kategorija="Pavārgrāmatas"><virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts><autors>Juris Kālis</autors><gads>2005</gads><cena>15.00</cena>

</gramata>

select Extract(OBJECT_VALUE, '/gramatveikals/gramata/autors[1]') from GRAMATVEIKALS;

<autors>Juris Kālis</autors><autors>Anita Zemniece</autors>

6

Page 7: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

SQL funkcija ExistsNode() noskaidro vai XPath norādei atbilst kaut viens elements, tad vērtība ir 1, citādi 0.

select ExistsNode(OBJECT_VALUE, '/gramatveikals/gramata [autors="Anita Zemniece"]')from GRAMATVEIKALS;

select Extract(OBJECT_VALUE, '//gramata/virsraksts/text()') "GRAMATA"from GRAMATVEIKALSwhere ExistsNode(OBJECT_VALUE, '//gramata[autors="Juris Kālis"]') = 1;

select Extract(OBJECT_VALUE, '/gramatveikals/gramata/virsraksts/text()') "VIRSRAKSTI"from GRAMATVEIKALSwhere ExistsNode(OBJECT_VALUE, '/gramatveikals/gramata/virsraksts[@valoda="latviešu"]') = 1;

select Extract(OBJECT_VALUE, '/gramatveikals/gramata /autors')from GRAMATVEIKALSwhere ExistsNode(OBJECT_VALUE, '/gramatveikals/gramata[autors="Anita Zemniece"]') = 1;

<autors>Juris Kālis</autors><autors>Anita Zemniece</autors>

7

Page 8: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

XMLType metodes getCLOBVal() izmantošana

select x.OBJECT_VALUE.getCLOBVal('/gramatveikals/gramata/autors') from GRAMATVEIKALS x; X.OBJECT_VALUE.GETCLOBVAL()-------------------------------------------------------------------------<?xml version="1.0" encoding="WINDOWS-1257"?><!-- New document created with EditiX at Fri Dec 02 17:23:04 EET 2011 --><gramatveikals> <gramata kategorija="Pavārgrāmatas"> <virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts> <autors>Juris Kālis</autors> <gads>2005</gads> <cena>15.00</cena> </gramata> <gramata kategorija="Daiļliteratūra"> <virsraksts valoda="latviešu">Biezais sniegs</virsraksts> <autors>Anita Zemniece</autors> <gads>2008</gads> <cena>10.00</cena> </gramata></gramatveikals>

8

Page 9: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

SQL funkcija ExtractValue() atgriež viena elementa vērtību

Atgriež tikai viena elementa vērtību, ja vaicājuma rezultātā tiek iegūtas vairākas vērtības – kļūda.SQL function ExtractValue takes as parameters an XMLType instance and an XPath expression that targets a node set.

select ExtractValue(OBJECT_VALUE, '/gramatveikals/gramata[autors="Juris Kālis"]/cena/text()') "GRĀMATA"from GRAMATVEIKALS;

select extractValue(OBJECT_VALUE, '/gramatveikals/gramata') "GRĀMATAS"from GRAMATVEIKALSwhere XMLExists('/gramatveikals/gramata[@kategorija="Daiļliteratūra"]', PASSING by VALUE OBJECT_VALUE);

Function ExtractValue and XMLType method getStringVal() differ in their treatment of entity encoding. Function extractValue unescapes any encoded entities; method getStringVal() returns the data with entity encoding intact.

9

Page 10: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

Funkcijas XMLExists() izmantošana XML dokumenta (node) atrašanai

XMLExists() funkcija noskaidro vai pieprasītais elements eksistē, tad vērtība ir TRUE.

select XMLExists('/gramatveikals' PASSING by VALUE OBJECT_VALUE)from GRAMATVEIKALSwhere XMLExists('/gramatveikals' PASSING by VALUE OBJECT_VALUE);

10

Page 11: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

Accessing fragments or nodes of an XML document using XMLQuery

You can use SQL/XML function XMLQuery to extract the nodes that match an XPath expression. The result is returned as an instance of XMLType.

select XMLQuery('/gramatveikals/gramata/ autors' PASSING OBJECT_VALUE RETURNING CONTENT)from GRAMATVEIKALS;

select XMLQuery('for $x in /gramatveikals/gramatawhere $x/autors="Juris Kālis"return $x/cena'passing OBJECT_VALUE RETURNING CONTENT) CENAfrom GRAMATVEIKALS; 

select XMLQuery('for $x in /gramatveikals/gramatawhere $x/autors="Juris Kālis"return <DETAĻAS>{$x/cena}</DETAĻAS>'passing OBJECT_VALUE RETURNING CONTENT) CENAfrom GRAMATVEIKALS; 

11

Page 12: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

select XMLQuery('for $x in /gramatveikals/gramatawhere $x/autors="Juris Kālis"return <DETAĻAS>{if ($x/cena="15.00") then "piecpadsmit" else "cita"}</DETAĻAS>'passing OBJECT_VALUE RETURNING CONTENT) CENAfrom GRAMATVEIKALS; 

12

Page 13: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

Query XMLType relational data, possibly decomposing the resulting XML into relational data using function XMLTable.

13

Page 14: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

XMLCAST SQL Function.Extracting the scalar value of an XML fragment using XMLCAST

SQL/XML standard function XMLCast casts its first argument to the scalar SQL data type specified by its second argument. The first argument is a SQL expression that is evaluated. Data types NUMBER, VARCHAR2, and any of the date and time data types can be used as the second argument.Unlike the SQL/XML standard, Oracle XML DB limits the use of XMLCast to cast XML to a SQL scalar data type; it does not support casting XML to XML or from a scalar SQL type to XML.The result of evaluating the first XMLCast argument is an XML value. It is converted to the target SQL data type by using the XQuery atomization process and then casting the XQuery atomic values to the target data type. If this conversion fails, then an error is raised. If conversion succeeds, the result returned is an instance of the target data type.Standard function XMLCast is similar to Oracle function extractValue, but it differs in that extractValue does not allow or require you to specify a target data type. In this, extractValue can sometimes be more convenient. XMLCast gives you the advantage of control over the data type, in addition to portability. If the SQL scalar data type cannot be determined at compile time, extractValue returns a value of time VARCHAR2(4000), which might not always be what you expect or want. You can work around this obstacle by using the SQL function cast, but XMLCast is a better choice in this case.

14

Page 15: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

Extracting the scalar value of an XML fragment using XMLCAST

This query extracts the scalar value of the "cena" node.

select XMLCast(XMLQuery('/gramatveikals/gramata/cena' PASSING OBJECT_VALUE RETURNING CONTENT)as VARCHAR2(4)) "CENA"from GRAMATVEIKALS;

select XMLCast(XMLQuery('/gramatveikals/gramata' PASSING OBJECT_VALUE RETURNING CONTENT)as VARCHAR2(100)) "GRĀMATA"from GRAMATVEIKALS;

15

Page 16: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

16

Page 17: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

Searching an XML Document using XMLEXISTS, XMLCast, and XMLQuery

SQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression. Function XMLExists returns a Boolean value of true if the document contains the node specified by the XPath expression supplied to the function and a value of false if it does not. Since XPath expressions can contain predicates, XMLExists can determine whether or not a given node exists in the document, and whether or not a node with the specified value exists in the document.Similarly, you can use SQL/XML functions XMLCast and XMLQuery in a SQL WHERE clause to limit the query results to documents that satisfy some property.

SELECT count(*) FROM purchaseorderWHERE XMLExists('$p/PurchaseOrder/Reference' PASSING OBJECT_VALUE AS "p");

SELECT count(*) FROM purchaseorderWHERE XMLExists('$p/PurchaseOrder[Reference="SBELL-2002100912333601PDT"]' PASSING OBJECT_VALUE AS "p");

SELECT XMLCast(XMLQuery('$p/PurchaseOrder/Reference' PASSING OBJECT_VALUE AS "p" RETURNING CONTENT) AS VARCHAR2(30))FROM purchaseorderWHERE XMLCast(XMLQuery('$p/PurchaseOrder/User' PASSING OBJECT_VALUE AS "p" RETURNING CONTENT) AS VARCHAR2(30)) LIKE 'S%';

17

Page 18: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

SQL operations on XMLType fragments using XMLTable

XMLTable extract an XMLType instance that contains the node or nodes that match an XPath expression. When the document contains multiple nodes that match the supplied XPath expression, such a query returns an XML fragment that contains all of the matching nodes. Unlike an XML document, an XML fragment has no single element that is the root element.This kind of result is common in these cases:1) when you retrieve the set of elements contained in a collection, in which case all nodes in the fragment are of the same type;

select b.COLUMN_VALUEfrom GRAMATVEIKALS a, XMLTable('/gramatveikals/gramata' PASSING a.OBJECT_VALUE) b;

2) when the target XPath expression ends in a wildcard, in which case the nodes in the fragment can be of different types.

select b.COLUMN_VALUEfrom GRAMATVEIKALS a, XMLTable('/gramatveikals/gramata/*' PASSING a.OBJECT_VALUE) b;

18

Page 19: XML datu izgūšana no XMLType tipa kolonām · Web viewSQL/XML standard function XMLExists evaluates whether or not a given document contains a node that matches a W3C XPath expression.

XMLTable converts XMLType object in virtual table with columns. Each XMLType instance in the virtual table contains a single element. You can use the COLUMNS clause of XMLTable to break up the data targeted by the XPath expression.

select kolonas.virsraksts, kolonas.gadsfrom GRAMATVEIKALS, XMLTable('for $i in /gramatveikals/gramata return $i' PASSING OBJECT_VALUE COLUMNS virsraksts varchar(30) PATH 'virsraksts', gads varchar2(10) PATH 'gads') kolonas ;

19