Академический Документы
Профессиональный Документы
Культура Документы
Introduction
Oracle ACE Director
Author
Data Architect for JP Morgan Chase
Twitter: @oracle_ace
Blogger
An Expert's Guide to Oracle Technology
http://it.toolbox.com/blogs/oracle-guide
Developers
3
Agenda
What is XML?
What is the XMLType data type? What is SQL/XML? What should(nt) I do with XML?
What is XML?
4
<?XML?>
What is XML?
5
Semi-Structured
Markup Language
What is XML?
6
Elements
Some nodes contain elements Some nodes contain character data A node can contain both
Namespaces
What is XML?
7
All open tags have closing tags All attribute values are enclosed in quotes
If a document is not well formed, it is not XML A well formed document may not be a VALID document
Valid XML
Conforms to a specific specification (DTD, XSD, RNG) A valid document will always be a well formed document
What is XML?
8
Simple Example
Element/Tag
<rootnode> <childnode1>Some Data</childnode1> Childnode1 is Sibling to <childnode2>Some more childnode2 <additional>data</additional></childnode2> <emptytag /> Closing Empty </rootnode> Tag
Tag
What is XML?
9
Namespaces
What is XML?
10
Simple Example
Namespace Identifier
Namespace Name
Namespace URI
<rootnode xmlns:abc='http://abc.org/xml' > <abc:xml:childnode1>Some Data</abc:childnode1> <abc:childnode2>Some more Namespace <additional>data</additional> Usage </abc:childnode2> Default <emptytag /> Usage </rootnode>
What is XML?
11
Describing XML
What is XML?
12
DTD
A list of valid elements and attributes May be inline or external Original descriptive language Limited and mostly obsolete No data type definitions No support for Namespaces
DTD Example
13
<!DOCTYPE note [ < !ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>
What is XML?
14
XSD
The XML Schema is the W3C replacement to DTDs XSD supports data types an namespaces XML Schemas are defined as XML Allows you to define ordering/number of elements Allows you to define mandatory elements XML Schemas are extensible
XSD Example
15
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="product" type="ProductType"/> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="xsd:integer"/> <xsd:element name="size" type="SizeType"/> </xsd:sequence> <xsd:attribute name="effDate" type="xsd:date"/> </xsd:complexType> <xsd:simpleType name="SizeType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="2"/> <xsd:maxInclusive value="18"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
What is XML?
16
RELAX NG
XML and non-XML formats Simpler than XSD More like a speaking dialect than an XML dialect Supports data types and namespaces Not as robust as XSD (fewer data types, not as many rules (defaults and such) Not as widely utilized as XSD
<element name="patron" xmnln="http://relaxng.org/ns/structure/1.0"> <interleave> <element name="name"><text/></element> <element name="id-num"><text/></element> <zeroOrMore> <element name="book"> <choice> <attribute name="isbn"/> <attribute name="title"/> </choice> </element> </zeroOrMore> </interleave> </element>
element patron { element name { text } & element id-num { text } & element book { (attribute isbn { text } | attribute title { text } ) }* }
Clob based
Well formed check Validating and Non-Validating
XML Document <onlyoneroot> data goes here </onlyoneroot> XML Content (fragment)
<name>Lewis</name><sex>yes</sex>
What is SQL/XML?
22
SQL/ XML
What is SQL/XML?
23
What is SQL/XML?
24
SQL/XML defines a set of mappings and a set of functions Based on XQuery and XPath
Oracle implements the core functions of SQL/XML XQuery is also supported
What is SQL/XML?
SQL/X Functions
25
XMLParse XMLSerialize XMLSequence XMLTable XMLElement XMLForest XMLAgg XML Comment XMLConcat More
What is SQL/XML?
26
Sample data
CREATE TABLE EMP ( LAST_NAME VARCHAR2(50), EMP_ID NUMBER NOT NULL, FIRST_NAME VARCHAR2(50), DEPT_ID NUMBER, SALARY NUMBER, CONSTRAINT EMP_pkey PRIMARY KEY (EMP_ID) )
What is SQL/XML?
Sample sample data:
27
INSERT INTO EMP( LAST_NAME, EMP_ID, FIRST_NAME, DEPT_ID, SALARY) VALUES ('Blow', 1, 'Joe', 1, 10000); Also INSERT..... ('Head', 2, 'Helmut', 1, 12000), ('Jack', 3, 'Noe', 1, 12000), ('Hard', 4, 'Blow', 2, 20000), ('First', 5, 'Hugo', 2, 21000), ('Spaem',6, 'Kingoof', 2, 20000), ('Ventura', 7, 'Ace', 3, 35000), ('Nickleby', 8, 'Nick', 3, 400000), ('Budd', 9, 'Billy', 4, 99000), ('Cleaver', 10, 'Wally', 4, 100000) ;
The XMLType constructor, or the SQL/XML function XMLParse, will convert text (or clobs) into XML
vXMLVar XMLType; vXMLVar := XMLType(<root>data</root>); vXMLVar := XMLParse(DOCUMENT <root>data</root>);
XMLSerialize turns XML into text (you can also use XMLType.toString) vString := XMLSerialize( DOCUMENT v_xml AS VARCHAR2); vString := XMLSerialize( CONTENT v_xml AS CLOB);
SELECT XMLElement(name main, XMLForest(last_name || ',' || first_name AS fullname, salary) ) FROM emp;
<MAIN> <FULLNAME>Blow,Joe</FULLNAME> <SALARY>10000</SALARY> </MAIN> <MAIN> <FULLNAME>Head,Helmut</FULLNAME> <SALARY>12000</SALARY> </MAIN>
SELECT XMLElement(name main, XMLElement(name fullname, XMLAttributes(dept_id), last_name || ',' || first_name ), XMLForest(salary) ) FROM emp;
SELECT XMLElement(name main, XMLComment('Comment goes here'), XMLForest(last_name, first_name)) FROM emp; <MAIN> <!--Comment goes here--> <LAST_NAME>Blow</LAST_NAME> <FIRST_NAME>Joe</FIRST_NAME> </MAIN>
SELECT XMLConcat( XMLElement(name lastname, last_name), XMLElement(name firstname, first_name) ) FROM emp;
<LASTNAME>Blow</LASTNAME><FIRSTNAME>Joe</FIRS TNAME> <LASTNAME>Head</LASTNAME><FIRSTNAME>Helmut</F IRSTNAME>
SELECT XMLElement(name main, XMLConcat( XMLElement(name lastname, last_name), XMLElement(name firstname, first_name) ) ) FROM emp; <MAIN><LASTNAME>Blow</LASTNAME><FIRSTNAM E>Joe</FIRSTNAME></MAIN> <MAIN><LASTNAME>Head</LASTNAME><FIRSTNAM E>Helmut</FIRSTNAME></MAIN>
XML
Whatshould(nt)IdowithXML?
43
Your XML is read only Your XML is transient Your XML is fairly static Your XML is very small You have a discrete key external to the XML Preserved white space is critical
Map your relational schema to the XML by its Schema or DTD Use XPath to extract columnar data
Use by some programming languages CMS systems Very unstructured data For reporting Web 2.0 (AJAX, SOAP, Etc)
Publish the XML spec Include a version element or attribute Change parameters and let users adapt over time Many web apps support XML XML + XSLT = Web Happiness Platform independent Current parsers are fast Validation and versioning built-in Public Specifications
Data interfaces