<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>ronin: Postgres + Xml Part 1</title>
    <link>http://blogs.divisibleprime.com/ronin/articles/2006/08/22/postgres-xml-part-1</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Stuff</description>
    <item>
      <title>Postgres + Xml Part 1</title>
      <description>&lt;p&gt;Well, it&amp;#8217;s tomorrow(plus a few days) so here is a quick intro to using the xml features in Postgres.&lt;/p&gt;

&lt;p&gt;First, we need a table, and some data:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;create table xml_test (
    id int not null,
    xml text not null
);

insert into xml_test(id, xml) values (1, 
    '&amp;lt;root&amp;gt;
       &amp;lt;child att="a" &amp;gt;child a&amp;lt;/child&amp;gt;
       &amp;lt;child att="b" &amp;gt;child b&amp;lt;/child&amp;gt;
     &amp;lt;/root&amp;gt;');
insert into xml_test(id, xml) values (2, 
     '&amp;lt;root&amp;gt;
        &amp;lt;child att="c" &amp;gt;child c&amp;lt;/child&amp;gt;
        &amp;lt;child att="d"&amp;gt;child d&amp;lt;/child&amp;gt;
      &amp;lt;/root&amp;gt;');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To find all the functions you can use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;\df
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A couple of useful functions are xpath_bool, which returns true when an xpath query has a result; and xpath_nodeset, which returns a nodeset(go figure) from an xpath.&lt;/p&gt;

&lt;p&gt;Lets see how xpath_bool works:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;select id 
  from xml_test 
  where xpath_bool(xml, '/root/child[@att="a"]');
 id
 ----
  1
 (1 row)

select id 
  from xml_test 
  where xpath_bool(xml, '/root/child[@att="c"]');
 id
 ----
  2
 (1 row)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and xpath_nodeset doesn&amp;#8217;t hold many surprises: &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;select xpath_nodeset(xml, '//root/child') 
  from xml_test;
 xpath_nodeset
 --------------------------------------------------------------
  &amp;lt;child att="a"&amp;gt;child a&amp;lt;/child&amp;gt;
    &amp;lt;child att="b"&amp;gt;child b&amp;lt;/child&amp;gt;
  &amp;lt;child att="c"&amp;gt;child c&amp;lt;/child&amp;gt;
    &amp;lt;child att="d"&amp;gt;child d&amp;lt;/child&amp;gt;
 (2 rows)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;putting these together:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;select xpath_nodeset(xml, '//root/child') 
  from xml_test 
  where
    xpath_bool(xml, '/root/child[@att="a"]');
 xpath_nodeset
 --------------------------------------------------------------
  &amp;lt;child att="a"&amp;gt;child a&amp;lt;/child&amp;gt;&amp;lt;child att="b"&amp;gt;child b&amp;lt;/child&amp;gt;
 (1 row)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Pretty straight forward stuff.&lt;/p&gt;

&lt;p&gt;Note: Excuse the crazy formatting, the default typo theme is whack.&lt;/p&gt;</description>
      <pubDate>Tue, 22 Aug 2006 18:18:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:8ab7600a-fb2f-418a-915b-6a2af6d696c4</guid>
      <author>Kerry</author>
      <link>http://blogs.divisibleprime.com/ronin/articles/2006/08/22/postgres-xml-part-1</link>
      <category>Tech</category>
      <category>Postgresql</category>
    </item>
  </channel>
</rss>
