56
SQL/XML for Developers Lewis Cunningham Senior Solutions Architect EnterpriseDB [email protected] om 1

SQL/XML For Postgres Developers

Embed Size (px)

DESCRIPTION

This is a presentation I have at the Postgres East 2008 conference in Baltimore, MD on March 30, 2008.

Citation preview

Page 1: SQL/XML For Postgres Developers

SQL/XML for Developers

Lewis CunninghamSenior Solutions [email protected]

m1

Page 2: SQL/XML For Postgres Developers

Postgres Rocks!

Agenda

What is XML?

What is the XML data type?

What is SQL/XML?

What should(n’t) I do with XML?

2

Page 3: SQL/XML For Postgres Developers

What is XML?

<?XML?>

3

Page 4: SQL/XML For Postgres Developers

What is XML?

Semi-Structured Hierarchical Not relational Not freeform

Markup “Language” Tags Identify Data <NAME>Lewis</NAME>

Human AND machine readable

4

Page 5: SQL/XML For Postgres Developers

What is XML?

Elements Root Element Child Elements

Elements are nodes Some nodes contain elements Some nodes contain character data A node can contain both

Namespaces

5

Page 6: SQL/XML For Postgres Developers

What is XML?

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

6

Page 7: SQL/XML For Postgres Developers

What is XML?

Simple Example

<rootnode><childnode1>Some Data</childnode1><childnode2>Some more

<additional>data</additional></childnode2><emptytag />

</rootnode>

Element/Tag

ClosingTag

Childnode1Child to rootnode

EmptyTag

Childnode1 isSibling tochildnode2

7

Page 8: SQL/XML For Postgres Developers

What is XML?

Namespaces

A namespace prevents naming collisions

A namespace provides clarity

A namespace allows multiple documents to be combined into a single document

8

Page 9: SQL/XML For Postgres Developers

What is XML?

Simple Example

<rootnode xmlns:abc='http://abc.org/xml' ><abc:xml:childnode1>Some Data</abc:childnode1><abc:childnode2>Some more

<additional>data</additional> </abc:childnode2>

<emptytag /></rootnode>

NamespaceIdentifier

NamespaceUsage

NamespaceName

NamespaceURI

Default Usage

9

Page 10: SQL/XML For Postgres Developers

What is XML?

Describing XML

DTD – Document Type Description

XSD – XML Schema

Relax NG – REgular LAnguage for Xml Next Generation

10

Page 11: SQL/XML For Postgres Developers

What is XML?

DTD

A list of valid elements and attributesMay be inline or externalOriginal descriptive languageLimited and mostly obsoleteNo data type definitionsNo support for Namespaces

11

Page 12: SQL/XML For Postgres Developers

DTD Example

<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>

12

Page 13: SQL/XML For Postgres Developers

What is XML?

XSD

The XML Schema is the W3C replacement to DTDs

XSD supports data types an namespacesXML Schemas are defined as XMLAllows you to define ordering/number of

elementsAllows you to define mandatory elementsXML Schemas are extensible

13

Page 14: SQL/XML For Postgres Developers

XSD Example

<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>

14

Page 15: SQL/XML For Postgres Developers

What is XML?

RELAX NG

XML and non-XML formatsSimpler than XSDMore like a speaking dialect than an XML

dialectSupports data types and namespacesNot as robust as XSD (fewer data types, not as

many rules (defaults and such)Not as widely utilized as XSD

15

Page 16: SQL/XML For Postgres Developers

RELAX NG (XML) Example

<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>

16

Page 17: SQL/XML For Postgres Developers

RELAX NG (Compact) Example

element patron { element name { text } & element id-num { text } & element book { (attribute isbn { text } | attribute title { text } ) }* }

17

Page 18: SQL/XML For Postgres Developers

What is the XML data type?

Postgres 8.3 Data Type

Text based

Well formed check

Non-validating

18

Page 19: SQL/XML For Postgres Developers

What is the XML data type?

XML Document <onlyoneroot>

data goes here</onlyoneroot>

XML Content (fragment)<name>Lewis</name><sex>yes</sex>

19

Page 20: SQL/XML For Postgres Developers

What is the XML data type?

Declare a column as XMLcreate table xml_tab (

id integer,data XML );

Declare a variable as XMLDECLARE v_xml XML;BEGIN……

20

Page 21: SQL/XML For Postgres Developers

What is SQL/XML?

SQL/XML

21

Page 22: SQL/XML For Postgres Developers

What is SQL/XML?

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

22

Page 23: SQL/XML For Postgres Developers

What is SQL/XML?

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

23

Page 24: SQL/XML For Postgres Developers

What is SQL/XML?

SQL/X Functions XMLParse XMLSerialize XMLElement XMLForest XMLAgg XMLAttributes XML Comment XMLConcat Xpath

24

Page 25: SQL/XML For Postgres Developers

What is SQL/XML?

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);

25

Page 26: SQL/XML For Postgres Developers

What is SQL/XML?

Sample tableINSERT 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) ;

26

Page 27: SQL/XML For Postgres Developers

What is SQL/XML? - XML Parse

XMLParse turns text into XML

vXMLVar := XMLParse(CONTENT ‘<root>data</root>’);

vXMLVar := XMLParse(DOCUMENT ‘<root>data</root>’);

27

Page 28: SQL/XML For Postgres Developers

What is SQL/XML? - XMLSerialize

XMLSerialize turns XML into text

vString := XMLSerialize( DOCUMENT v_xml AS TEXT);

vString := XMLSerialize( CONTENT v_xml AS TEXT);

28

Page 29: SQL/XML For Postgres Developers

What is SQL/XML? - XMLElement

SELECT XMLElement(name main, last_name) from emp;

<main>Blow</main>

<main>Head</main>

<main>Jack</main>

<main>Hard</main>

<main>First</main>

29

Page 30: SQL/XML For Postgres Developers

What is SQL/XML? – XMLElement Cont’d

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>

30

Page 31: SQL/XML For Postgres Developers

What is SQL/XML? - XMLForest

SELECT 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>

31

Page 32: SQL/XML For Postgres Developers

What is SQL/XML? – XMLForest Cont’d

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>

32

Page 33: SQL/XML For Postgres Developers

What is SQL/XML? – XMLAgg

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>…

33

Page 34: SQL/XML For Postgres Developers

What is SQL/XML? – XMLAgg Cont'd

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>…

34

Page 35: SQL/XML For Postgres Developers

What is SQL/XML? - XMLAttributes

SELECT XMLElement(name main, XMLAttributes(nextval('t_seq') AS rownum) )

FROM emp;

<main rownum="1"/>

<main rownum="2"/>

<main rownum="3"/>

<main rownum="4"/>

35

Page 36: SQL/XML For Postgres Developers

What is SQL/XML? – XMLAttributes Cont’d

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;

36

Page 37: SQL/XML For Postgres Developers

What is SQL/XML? – XMLAttributes Cont’d

<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>

37

Page 38: SQL/XML For Postgres Developers

What is SQL/XML? – Concatenating Columns

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>

38

Page 39: SQL/XML For Postgres Developers

What is SQL/XML? – Concat and Attributes

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>

39

Page 40: SQL/XML For Postgres Developers

What is SQL/XML? - XMLComment

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>

40

Page 41: SQL/XML For Postgres Developers

What is SQL/XML? - XMLConcat

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>

41

Page 42: SQL/XML For Postgres Developers

What is SQL/XML? – XMLConcat Cont'd

SELECT XMLConcat( XMLElement(name lastname, last_name), XMLElement(name firstname, first_name) )FROM emp;

<lastname>Blow</lastname><firstname>Joe</firstname>

<lastname>Head</lastname><firstname>Helmut</firstname>

42

Page 43: SQL/XML For Postgres Developers

What is SQL/XML? – XMLConcat Cont'd

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>

43

Page 44: SQL/XML For Postgres Developers

What is SQL/XML? – XML Tables

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);

44

Page 45: SQL/XML For Postgres Developers

What is SQL/XML? – XML Table

SELECT * FROM xmltab;

<main><lastname>Blow</lastname><firstname>Joe</firstname></main>

<main><lastname>Head</lastname><firstname>Helmut</firstname></main>

<main><lastname>Jack</lastname><firstname>Noe</firstname></main>

<main><lastname>Hard</lastname><firstname>Blow</firstname></main>

45

Page 46: SQL/XML For Postgres Developers

What is SQL/XML? - XPath

XPath is a language for navigating through nodes in an XML document

XPath is hierarchicalThink of XPath like navigating directories in a

file systemAn XPath expression may point to

A Node (like a directory) Data (like a file) Functions (like file properties or file values)

46

Page 47: SQL/XML For Postgres Developers

What is SQL/XML? – Xpath Cont'd

SELECT Xpath('/main/firstname/text()', col1) FROM xmltab;

xpath

-----------

{Joe}

{Helmut}

{Noe}

{Blow}

{Hugo}

47

Page 48: SQL/XML For Postgres Developers

What is SQL/XML? – Xpath Cont'd

SELECT textcol[1] FROM ( SELECT xpath('/main/firstname/text()', col1) AS

textcol FROM xmltab ) AS xmlsource;

textcol

---------

Joe

Helmut

Noe

Blow

Hugo

48

Page 49: SQL/XML For Postgres Developers

What is SQL/XML? – XML from a Query

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>

49

Page 50: SQL/XML For Postgres Developers

What is SQL/XML? – XML from a Table

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>

50

Page 51: SQL/XML For Postgres Developers

What should(n’t) I do with XML?

XML

51

Page 52: SQL/XML For Postgres Developers

What should(n’t) I do with XML?

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

52

Page 53: SQL/XML For Postgres Developers

What should(n’t) I do with XML? Cont'd

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

53

Page 54: SQL/XML For Postgres Developers

What should(n’t) I do with XML? Cont'd

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

54

Page 55: SQL/XML For Postgres Developers

What should(n’t) I do with XML? Cont'd

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

55

Page 56: SQL/XML For Postgres Developers

SQL/XML for Developers

Lewis CunninghamSenior Solutions [email protected]

m56