SQL XML For Developers in PostgreSQL
SQL XML For Developers in PostgreSQL
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
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
Text based
Well formed check
Non-validating
What is SQL/XML?
SQL/ XML
What is SQL/XML?
SQL/XML (or SQL/X) is standards based
What is SQL/XML?
SQL/XML defines a set of mappings and a
set of functions
Based on XQuery and XPath
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) ;
INSERT INTO xmltab ( SELECT XMLElement(name main, XMLConcat( XMLElement(name lastname, last_name), XMLElement(name firstname, first_name) ) ) FROM emp);
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)
FROM xmltab;
xpath ----------{Joe} {Helmut} {Noe} {Blow} {Hugo}
XML
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
document