SQL/XML for Developers
Lewis Cunningham
Senior Solutions Architect
EnterpriseDB
[email protected]
1
Postgres Rocks!
2
Agenda
What is XML?
What is the XML data type?
What is SQL/XML?
What should(n’t) I do with XML?
What is XML?
3
<?XML?>
What is XML?
4
Semi-Structured
Hierarchical
Not relational
Not freeform
Markup “Language”
Tags Identify Data
<NAME>Lewis</NAME>
Human AND machine readable
What is XML?
5
Elements
Root Element
Child Elements
Elements are nodes
Some nodes contain elements
Some nodes contain character data
A node can contain both
Namespaces
What is XML?
6
Well Formed XML
Follows XML formatting rules
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?
7
Element/Tag
Simple Example Childnode1
Child to
rootnode
<rootnode>
<childnode1>Some Data</childnode1> Childnode1 is
Sibling to
<childnode2>Some more childnode2
<additional>data</additional></childnode2>
<emptytag />
Closing
</rootnode> Empty
Tag
Tag
What is XML?
8
Namespaces
A namespace prevents naming collisions
A namespace provides clarity
A namespace allows multiple documents to be
combined into a single document
What is XML?
9
Namespace Namespace Namespace
Simple Example Identifier Name 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>
<emptytag /> Default
Usage
</rootnode>
What is XML?
10
Describing XML
DTD – Document Type Description
XSD – XML Schema
Relax NG – REgular LAnguage for Xml Next
Generation
What is XML?
11
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
12
<!DOCTYPE note [ <
!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
What is XML?
13
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
14
<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?
15
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
RELAX NG (XML) Example
16
<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>
RELAX NG (Compact) Example
17
element patron {
element name { text } &
element id-num { text } &
element book {
(attribute isbn { text } |
attribute title { text } )
}*
}
What is the XML data type?
18
Postgres 8.3 Data Type
Text based
Well formed check
Non-validating
What is the XML data type?
19
XML Document
<onlyoneroot>
data goes here
</onlyoneroot>
XML Content (fragment)
<name>Lewis</name><sex>yes</sex>
What is the XML data type?
20
Declare a column as XML
create table xml_tab (
id integer,
data XML );
Declare a variable as XML
DECLARE
v_xml XML;
BEGIN
……
What is SQL/XML?
21
SQL/
XML
What is SQL/XML?
22
SQL/XML (or SQL/X) is standards based
Combines XML and SQL
IBM, Oracle, Microsoft and Sybase all played a large
part in defining SQL/XML
Home on the web: www.sqlx.org
What is SQL/XML?
23
SQL/XML defines a set of mappings and a
set of functions
Based on XQuery and XPath
Postgres 8.3 implements the core functions
of SQL/XML
XQuery is not yet supported in Postgres
What is SQL/XML?
24
SQL/X Functions
XMLParse
XMLSerialize
XMLElement
XMLForest
XMLAgg
XMLAttributes
XML Comment
XMLConcat
Xpath
What is SQL/XML?
25
Sample data
CREATE TABLE EMP
(
LAST_NAME text,
EMP_ID integer NOT NULL,
FIRST_NAME text,
DEPT_ID integer,
SALARY integer,
CONSTRAINT EMP_pkey PRIMARY KEY (EMP_ID)
)
WITH (OIDS=FALSE);
What is SQL/XML?
26
Sample table
INSERT INTO EMP(
LAST_NAME, EMP_ID, FIRST_NAME, DEPT_ID, SALARY)
VALUES
('Blow', 1, 'Joe', 1, 10000),
('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) ;
What is SQL/XML? - XML Parse
27
XMLParse turns text into XML
vXMLVar :=
XMLParse(CONTENT ‘<root>data</root>’);
vXMLVar :=
XMLParse(DOCUMENT ‘<root>data</root>’);
What is SQL/XML? - XMLSerialize
28
XMLSerialize turns XML into text
vString := XMLSerialize(
DOCUMENT v_xml AS TEXT);
vString := XMLSerialize(
CONTENT v_xml AS TEXT);
What is SQL/XML? - XMLElement
29
SELECT XMLElement(name main, last_name) from
emp;
<main>Blow</main>
<main>Head</main>
<main>Jack</main>
<main>Hard</main>
<main>First</main>
What is SQL/XML? – XMLElement Cont’d
30
SELECT XMLElement(name main, last_name),
XMLElement(name main, first_name)
FROM emp;
<main>Blow</main> | <main>Joe</main>
<main>Head</main> | <main>Helmut</main>
<main>Jack</main> | <main>Noe</main>
<main>Hard</main> | <main>Blow</main>
What is SQL/XML? - XMLForest
31
SELECT XMLForest(last_name, first_name)
FROM emp;
<last_name>Blow</last_name><first_name>Joe</f
irst_name>
<last_name>Head</last_name><first_name>Helmut
</first_name>
What is SQL/XML? – XMLForest Cont’d
32
SELECT XMLElement(name main,
XMLForest(last_name, first_name) )
FROM emp;
<main>
<last_name>Blow</last_name>
<first_name>Joe</first_name>
</main>
<main>
<last_name>Head</last_name>
<first_name>Helmut</first_name>
</main>
What is SQL/XML? – XMLAgg
33
SELECT XMLAgg(
XMLForest(last_name, first_name) )
FROM emp;
<last_name>Blow</last_name>
<first_name>Joe</first_name>
<last_name>Head</last_name>
<first_name>Helmut</first_name>
<last_name>Jack</last_name>
<first_name>Noe</first_name>…
What is SQL/XML? – XMLAgg Cont'd
34
SELECT XMLElement(name main,
XMLAgg(XMLForest(last_name, first_name) ))
FROM emp;
<main>
<last_name>Blow</last_name>
<first_name>Joe</first_name>
<last_name>Head</last_name>
<first_name>Helmut</first_name>
<last_name>Jack</last_name>
<first_name>Noe</first_name>…
What is SQL/XML? - XMLAttributes
35
SELECT XMLElement(name main,
XMLAttributes(nextval('t_seq') AS rownum) )
FROM emp;
<main rownum="1"/>
<main rownum="2"/>
<main rownum="3"/>
<main rownum="4"/>
What is SQL/XML? – XMLAttributes Cont’d
36
CREATE TEMP SEQUENCE t_seq;
SELECT XMLElement(name main,
XMLAttributes(nextval('t_seq') AS rownum),
XMLForest(last_name, first_name) )
FROM emp;
DROP SEQUENCE t_seq;
What is SQL/XML? – XMLAttributes Cont’d
37
<main rownum="1">
<last_name>Blow</last_name>
<first_name>Joe</first_name>
</main>
<main rownum="2">
<last_name>Head</last_name>
<first_name>Helmut</first_name>
</main>
What is SQL/XML? – Concatenating Columns
38
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>
What is SQL/XML? – Concat and Attributes
39
SELECT XMLElement(name main,
XMLElement(name fullname,
XMLAttributes(dept_id), last_name || ',' || first_name ),
XMLForest(salary) ) FROM emp;
<main>
<fullname dept_id="1">
Blow,Joe</fullname>
<salary>10000</salary>
</main>
What is SQL/XML? - XMLComment
40
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>
What is SQL/XML? - XMLConcat
41
SELECT XMLElement(name lastname,
last_name),
XMLElement(name firstname, first_name)
FROM emp;
<lastname>Blow</lastname> |
<firstname>Joe</firstname>
<lastname>Head</lastname> |
<firstname>Helmut</firstname>
What is SQL/XML? – XMLConcat Cont'd
42
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>
What is SQL/XML? – XMLConcat Cont'd
43
SELECT XMLElement(name main,
XMLConcat(
XMLElement(name lastname, last_name),
XMLElement(name firstname, first_name) ) )
FROM emp;
<main>
<lastname>Blow</lastname>
<firstname>Joe</firstname>
</main>
What is SQL/XML? – XML Tables
44
CREATE TABLE xmltab (col1 XML);
INSERT INTO xmltab (
SELECT XMLElement(name main, XMLConcat(
XMLElement(name lastname, last_name),
XMLElement(name firstname, first_name) ) )
FROM emp);
What is SQL/XML? – XML Table
45
SELECT * FROM xmltab;
<main><lastname>Blow</lastname><firstname>Joe
</firstname></main>
<main><lastname>Head</lastname><firstname>Hel
mut</firstname></main>
<main><lastname>Jack</lastname><firstname>Noe
</firstname></main>
<main><lastname>Hard</lastname><firstname>Blo
w</firstname></main>
What is SQL/XML? - XPath
46
XPath is a language for navigating through nodes in
an XML document
XPath is hierarchical
Think of XPath like navigating directories in a file
system
An XPath expression may point to
A Node (like a directory)
Data (like a file)
Functions (like file properties or file values)
What is SQL/XML? – Xpath Cont'd
47
SELECT Xpath('/main/firstname/text()', col1)
FROM xmltab;
xpath
-----------
{Joe}
{Helmut}
{Noe}
{Blow}
{Hugo}
What is SQL/XML? – Xpath Cont'd
48
SELECT textcol[1] FROM (
SELECT xpath('/main/firstname/text()', col1) AS
textcol FROM xmltab ) AS xmlsource;
textcol
---------
Joe
Helmut
Noe
Blow
Hugo
What is SQL/XML? – XML from a Query
49
select query_to_xml('select * from emp', TRUE,
TRUE, '');
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<last_name>Blow</last_name>
<emp_id>1</emp_id>
<first_name>Joe</first_name>
<dept_id>1</dept_id>
<salary>10000</salary>
</row>
What is SQL/XML? – XML from a Table
50
select table_to_xml('emp', TRUE, TRUE, '');
<emp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<last_name>Blow</last_name>
<emp_id>1</emp_id>
<first_name>Joe</first_name>
<dept_id>1</dept_id>
<salary>10000</salary>
</emp>
What should(n’t) I do with XML?
51
XML
What should(n’t) I do with XML?
52
PostgreSQL is a RELATIONAL database
Store your data relationally, unless
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
What should(n’t) I do with XML? Cont'd
53
Convert your XML to Relations by Shredding
Map your relational schema to the XML by its Schema or DTD
Use XPath to extract columnar data
Use SQL/XML to recreate the original XML
document
What should(n’t) I do with XML? Cont'd
54
Relational data is much easier to
Index
Update
Manipulate
XML data is better for
Use by some programming languages
CMS systems
Very unstructured data
For reporting
What should(n’t) I do with XML? Cont'd
55
XML is great for
Public stored procedure interfaces
Publish the XML spec
Include a version element or attribute
Change parameters and let users adapt over time
Web based processing
Many web apps support XML
XML + XSLT = Web Happiness
Data interfaces
Platform independent
Current parsers are fast
Validation and versioning built-in
Public Specifications
SQL/XML for Developers
Lewis Cunningham
Senior Solutions Architect
EnterpriseDB
[email protected]
56