SQL/XML for Developers
Lewis Cunningham Senior Solutions Architect EnterpriseDB [email protected]
Postgres Rocks!
Agenda
What is XML?
What is the XML data type?
What is SQL/XML?
What should(nt) I do with XML?
What is XML?
<?XML?>
What is XML?
Semi-Structured Not relational Not freeform
Markup Language Tags Identify Data <NAME>Lewis</NAME>
Human AND machine readable
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
What is XML?
Simple Example
Element/Tag 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 the XML data type?
Postgres 8.3 Data Type
Text based
Well formed check
Non-validating
What is the XML data type?
XML Document
<onlyoneroot> data goes here </onlyoneroot>
XML Content (fragment) <name>Lewis</name><sex>yes</sex>
What is the XML data type?
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?
SQL/ XML
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
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
What is SQL/XML?
SQL/X Functions XMLParse XMLSerialize XMLElement XMLForest XMLAttributes XML Comment XMLConcat Xpath
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);
What is SQL/XML?
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
XMLParse turns text into XML
vXMLVar := XMLParse(CONTENT <root>data</root>); vXMLVar := XMLParse(DOCUMENT <root>data</root>);
What is SQL/XML? - XMLSerialize
XMLSerialize turns XML into text
vString := XMLSerialize( DOCUMENT v_xml AS TEXT);
vString := XMLSerialize( CONTENT v_xml AS TEXT);
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>
What is SQL/XML? XMLElement Contd
SELECT XMLElement(name main, last_name), XMLElement(name main, first_name) FROM emp;
<main>Blow</main> <main>Head</main> <main>Jack</main> <main>Hard</main> | | | | <main>Joe</main> <main>Helmut</main> <main>Noe</main> <main>Blow</main>
What is SQL/XML? - XMLForest
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 Contd
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? - XMLAttributes
SELECT XMLElement(name main, XMLAttributes(nextval('t_seq') AS rownum) ) FROM emp; <main <main <main <main rownum="1"/> rownum="2"/> rownum="3"/> rownum="4"/>
What is SQL/XML? XMLAttributes Contd
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 Contd
<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
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
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
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
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
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
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
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
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
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
SELECT Xpath('/main/firstname/text()', col1)
FROM xmltab;
xpath ----------{Joe} {Helmut} {Noe} {Blow} {Hugo}
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
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>
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>
What should(nt) I do with XML?
XML
What should(nt) 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
What should(nt) 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
What should(nt) 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
SQL/XML for Developers
Lewis Cunningham Senior Solutions Architect EnterpriseDB
[email protected]