schema registration - web view4 5 the . at. keyword can be used to count the iteration: ......

Post on 05-Feb-2018

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Using of Oxygen XML editorfor XML document creation

XML file<?xml version="1.0"?><SERVICES><SERVICE><SERVICE_DESCRIPTION>Coloring: short</SERVICE_DESCRIPTION><SERVICE_PRICE>16 eur</SERVICE_PRICE><SERVICE_DURATION>40</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Coloring: long</SERVICE_DESCRIPTION><SERVICE_PRICE>18 eur</SERVICE_PRICE><SERVICE_DURATION>40</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Haircut: short, simple</SERVICE_DESCRIPTION><SERVICE_PRICE>18 eur</SERVICE_PRICE><SERVICE_DURATION>40</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Haircut: short, difficult</SERVICE_DESCRIPTION><SERVICE_PRICE>22 eur</SERVICE_PRICE><SERVICE_DURATION>55</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Haircut + coloring: short, simple</SERVICE_DESCRIPTION><SERVICE_PRICE>24 eur</SERVICE_PRICE><SERVICE_DURATION>75</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Haircut + coloring: long, simple</SERVICE_DESCRIPTION>SERVICE_PRICE>26 eur</SERVICE_PRICE><SERVICE_DURATION>85</SERVICE_DURATION></SERVICE></SERVICES>

Create or replace directory MD4 as 'C:\db_4';

2

Schema generation

<?xml version="1.0" encoding="UTF-8"?><xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema"elementFormDefault="qualified"><xs:elementname="SERVICES"><xs:complexType><xs:sequence><xs:elementmaxOccurs="unbounded"ref="SERVICE"/></xs:sequence></xs:complexType></xs:element><xs:elementname="SERVICE"><xs:complexType><xs:sequence><xs:elementref="SERVICE_DESCRIPTION"/><xs:elementref="SERVICE_PRICE"/><xs:elementref="SERVICE_DURATION"/></xs:sequence></xs:complexType></xs:element><xs:elementname="SERVICE_DESCRIPTION"type="xs:string"/><xs:elementname="SERVICE_PRICE"type="xs:string"/><xs:elementname="SERVICE_DURATION"type="xs:integer"/></xs:schema>

3

Schema registration

There are two registration types.

1. Use of SQL Developer.

2. Use of PL/SQL language package DBMS_XMLSCHEMA procedure REGISTERSCHEMA.

begin DBMS_XMLSCHEMA.REGISTERSCHEMA(

SCHEMAURL => 'http://localhost:3333/public/services.xsd',SCHEMADOC =>bfilename ('MD4', 'services.xsd'),CSID =>nls_charset_id ('AL32UTF8'),

options =>dbms_xmlschema.REGISTER_BINARYXML);end;

4

SELECT SCHEMA_URL FROM USER_XML_SCHEMAS;

5

Creation of XMLType type tables1. Structured storage

createtable SERVICES_STRUCTURED of XMLTypexmltype store as object relationalxmlschema XML_SCHEMA_SERVICES element "SERVICES";

2. Unstructured storage without schema

createtable SERVICES_NONSTRUCTURED of xmltype xmltype store as clob;

6

3. Unstructured storage with schema

createtable SERVICES_STRUCTURED of xmltypexmltype store as clob xmlschema XML_SCHEMA_SERVICES element "SERVICES";

4. Binary storage without schema

create table SERVICES_BINARY of XMLTypeXMLType store as BINARY XML;

7

Meta data output

setlinesize 100;

select table_name, table_typefrom user_all_tableswhere table_name in(

select object_name from user_objects where (created>= to_date('13-12-2015', 'DD-MM-YYYY')) and (object_type = 'TABLE'));

8

XML data input

insert into SERVICES_NONSTRUCTURED values(xmltype('<services><SERVICE><SERVICE_DESCRIPTION>Coloring: short</SERVICE_DESCRIPTION><SERVICE_PRICE>16 eur</SERVICE_PRICE><SERVICE_DURATION>40</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Coloring: long</SERVICE_DESCRIPTION><SERVICE_PRICE>18 eur</SERVICE_PRICE><SERVICE_DURATION>40</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Haircut: short, simple</SERVICE_DESCRIPTION><SERVICE_PRICE>18 eur</SERVICE_PRICE><SERVICE_DURATION>40</SERVICE_DURATION></SERVICE>

<SERVICE><SERVICE_DESCRIPTION>Haircut: short, difficult</SERVICE_DESCRIPTION>SERVICE_PRICE>22 eur</SERVICE_PRICE><SERVICE_DURATION>55</SERVICE_DURATION></SERVICE>

</services>'));

9

insert into SERVICES_NONSTRUCTURED_SCH values(xmltype(bfilename('MD4','services.xml'),nls_charset_id('AL32UTF8')));

10

insertinto SERVICES_STRUCTURED values(xmltype(bfilename('MD4','services.xml'),nls_charset_id('AL32UTF8')));

11

insertinto SERVICES_BINARY values(xmltype(bfilename('MD4','services.xml'),nls_charset_id('AL32UTF8')));

12

Extraction of XML data

select object_value from SERVICES_STRUCTURED;

select object_value.getclobval from SERVICES_NONSTRUCTURED;

select existsnode(object_value, '/services/SERVICE/SERVICE_DURATION="40"') from SERVICES_NONSTRUCTURED;

13

select existsnode(object_value, '/services/SERVICE/SERVICE_PRICE="18 eur"') from SERVICES_BINARY;

select extract(object_value, '/SERVICES/SERVICE[2]/SERVICE_PRICE'),extract(object_value, '/SERVICES/SERVICE[2]/SERVICE_DURATION')from SERVICES_STRUCTURED;

14

SERVICES_NONSTRUCTURED_SCH:select extract(object_value, '/SERVICES/SERVICE[last()]/*')from SERVICES_NONSTRUCTURED_SCH;

15

XQuery

XQuery is the language for querying XML dataXQuery is built on XPath expressionsXQuery is supported by all major databases

16

FLWOR?

FLWOR (pronounced "flower") is an acronym for "For, Let, Where, Order by, Return".For - selects a sequence of nodesLet - binds a sequence to a variableWhere - filters the nodesOrder by - sorts the nodesReturn - what to return (gets evaluated once for every node)

for $x in doc("books.xml")/bookstore/bookwhere $x/price>30order by $x/titlereturn $x/title

for $x in (1 to 5)return <test>{$x}</test>

<test>1</test><test>2</test><test>3</test><test>4</test><test>5</test>

The at keyword can be used to count the iteration:for $x at $i in doc("books.xml")/bookstore/book/titlereturn <book>{$i}. {data($x)}</book> <book>1. Everyday Italian</book><book>2. Harry Potter</book><book>3. XQuery Kick Start</book><book>4. Learning XML</book>

17

for $x in (10,20), $y in (100,200)return <test>x={$x} and y={$y}</test>

<test>x=10 and y=100</test><test>x=10 and y=200</test><test>x=20 and y=100</test><test>x=20 and y=200</test>

The let clause allows variable assignments and it avoids repeating the same expression many times. The let clause does not result in iteration.let $x := (1 to 5)return <test>{$x}</test>

<test>1 2 3 4 5</test>

The where clause is used to specify one or more criteria for the result:where $x/price>30 and $x/price<100

The order by clause is used to specify the sort order of the result. Here we want to order the result by category and title:

for $x in doc("books.xml")/bookstore/bookorder by $x/@category, $x/titlereturn $x/title

<title lang="en">Harry Potter</title><title lang="en">Everyday Italian</title><title lang="en">Learning XML</title><title lang="en">XQuery Kick Start</title>

18

The return clause specifies what is to be returned.

for $x in doc("books.xml")/bookstore/bookreturn $x/title

<title lang="en">Everyday Italian</title><title lang="en">Harry Potter</title><title lang="en">XQuery Kick Start</title><title lang="en">Learning XML</title>

19

Use of functions in XQuery

doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']

let $name := (substring($booktitle,1,4))

XQuery user-defined functions

User-defined functions can be defined in the query or in a separate library.

declare function prefix:function_name($parameter as datatype)as returnDatatype{ ...function code here...};

Notes on user-defined functions: Use the declare function keywordThe name of the function must be prefixedThe data type of the parameters are mostly the same as the data types defined in XML SchemaThe body of the function must be surrounded by curly braces

Example of a User-defined Function Declared in the Query

declare function local:minPrice($p as xs:decimal?,$d as xs:decimal?)as xs:decimal?{let $disc := ($p * $d) div 100return ($p - $disc)};

Below is an example of how to call the function above:<minPrice>{local:minPrice($book/price,$book/discount)}</minPrice>

20

Data extraction with XmlQuery

select XmlQuery('for $x in /SERVICES/SERVICEwhere $x/SERVICE_PRICE="18 eur"return data($x/SERVICE_DESCRIPTION)'passing OBJECT_VALUE returning content) long_servicesfrom SERVICES_NONSTRUCTURED_SCH;

21

select XmlQuery('for $x in /SERVICES/SERVICEwhere $x/SERVICE_DESCRIPTION="Haircut: short, difficult"return data($x/SERVICE_PRICE)'PASSING OBJECT_VALUE RETURNING CONTENT) short_difficult_haircut_priceFROM SERVICES_BINARY;

22

PL/SQL queryOutput of elements SERVICE in *.txt file.Is used SQL*Plus data output function SPOOL.

set SERVEROUTPUT onspool S_DATA.txt;

DECLAREcursor S_CURSOR isselect extract(object_value, 'SERVICES/SERVICE')from SERVICES_STRUCTURED;

BEGIN OPEN S_CURSOR; LOOP FETCH S_CURSOR INTO S_DATA; EXIT WHEN S_CURSOR%NOTFOUND; DBMS_OUTPUT.PUT_LINE(S_DATA.getStringVal()); END LOOP; CLOSE S_CURSOR;END;

spooloff;

23

24

XML data output from relational table

create table SERVICES_REL (SERVICE_ID INT,SERVICE_DESCRIPTION VARCHAR2(40),SERVICE_PRICE VARCHAR2(10),SERVICE_DURATION INT);

begininsertinto SERVICES_REL values(1, 'Coloring: short','16 eur',40);insertinto SERVICES_REL values(2, 'Coloring: long','18 eur',40);insertinto SERVICES_REL values(3, 'Haircut: short, simple','18 eur',40);insertinto SERVICES_REL values(4, 'Haircut: short, difficult','22 eur',55);insertinto SERVICES_REL values(5, 'Haircut + coloring: short, simple','24 eur',75); insertinto SERVICES_REL values(6, 'Haircut + coloring: long, simple','26 eur',85);insertinto SERVICES_REL values(7, 'Haircut: long, simple','17 eur',35);insertinto SERVICES_REL values(8, 'Haircut: long, difficult','23 eur',50);insertinto SERVICES_REL values(9, 'Haircut + coloring: long, simple','30 eur',85);insertinto SERVICES_REL values(10, 'Haircut + coloring: long, difficult','35 eur',100);insertinto SERVICES_REL values(11, 'Hairstyling: short, simple','11 eur',30);insertinto SERVICES_REL values(12, 'Hairstyling: short, difficult','15 eur',40);insertinto SERVICES_REL values(13, 'Hairstyling: long, simple','15 eur',30);insertinto SERVICES_REL values(14, 'Hairstyling: long, difficult','18 eur',40);insertinto SERVICES_REL values(15, 'Hairstyle: wedding','35 eur',40);insertinto SERVICES_REL values(16, 'Hairstyle: dreadlocks','35 eur',45);end;

25

Relational dataExtracted dataXML data

Functions for transformation

Function DescriptionxmlElement() Returns an XML element in an XMLType when given the

XML element name, an optional list of XML attributes (XMLATTRIBUTES()), and an optional list of values as the content of the new element. XMLELEMENT() can also contain other XML elements or XML fragments (XMLFOREST() ) as its children.

xmlAttributes() Used within XMLELEMENT() to specify attributes for the element.

xmlForest() Returns an XML fragment in an XMLType when given a list of named expressions for the XML elements. Each expression specifies the name of an XML element and its content.

XMLCONCAT() Returns an XML fragment in an XMLType by concatenating a list of XML elements/values.

XMLAGG() Returns an XML fragment in an XMLType by aggregating XML fragments, with the option of XML element sorting.

SYS_xmlGen() Generates an XML document with the <?XML?> prolog from one scalar type, a user-defined object type, or an instance of XMLType.

XMLSEQUENCE() Returns a collection of XMLTypes in an XMLSEQUENCEType, which is a VARRAY of XMLType instances in the database.

SYS_XMLAGG() Aggregates XML elements from one scalar type, a user-defined object type, or an instance of XMLType.

XMLCOLATTVAL() Generates a set of <column/> elements with the name attributes specifying the column names or the name aliases.

UPDATEXML() Updates XML documents in XMLTypes using XPath expressions.

XMLTRANSFORM() Applies XSL transformation on XML documents in XMLTypes.

EXTRACTVALUE() Returns scalar content, such as numbers or strings, when passed an XPath expression pointing to an XML element with only a single text child.

setlinesize 500;

26

setserveroutputon;

select XMLELEMENT("SERVICE", XMLATTRIBUTES( a.SERVICE_ID as "ID"), XMLFOREST(a.SERVICE_DESCRIPTION as "DESCRIPTION", a.SERVICE_PRICE as "PRICE", a.SERVICE_DURATION as "DURATION ")).extract('/*') as xmlfrom SERVICES_REL a;

27

top related