<?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: Category Postgresql</title>
    <link>http://blogs.divisibleprime.com/ronin/articles/category/postgresql</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>
    <item>
      <title>Postgresql + XML + Gentoo</title>
      <description>&lt;p&gt;Quick howto on setting up the XML Support in Postgreqsql on Gentoo.&lt;/p&gt;

&lt;p&gt;Need to emerge postgresql with xml support on&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USE="xml" emerge -vp postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Make sure xml is being used.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[ebuild   R   ] dev-db/postgresql-8.0.8  USE="nls pam perl python 
    readline ssl xml zlib -doc -kerberos -libg++ -pg-hier 
    -pg-intdatetime -tcl -tk" 0 kB
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Restart postgresql.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/etc/init.d/postgresql restart
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Connect to the database as the admin user so you can install the Xml libraries.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;psql -U postgres &amp;lt;dbname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Install the libraries&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;\i /usr/share/postgresql/contrib/pgxml.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should see a whole bunch of CREATE FUNCTION lines echo out and then your good to go.&lt;/p&gt;

&lt;p&gt;Tomorrow: A quick look at what you can do to get started using it.  &lt;/p&gt;

&lt;p&gt;For those who want to get started straight away &lt;a href="http://www.throwingbeans.org/postgresql_and_xml.html"&gt;This&lt;/a&gt; site has the low down.&lt;/p&gt;</description>
      <pubDate>Wed, 16 Aug 2006 21:39:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:de6026fe-5769-4e92-8cd4-00b80284abc5</guid>
      <author>Kerry</author>
      <link>http://blogs.divisibleprime.com/ronin/articles/2006/08/16/postgresql-xml-gentoo</link>
      <category>Tech</category>
      <category>Postgresql</category>
    </item>
  </channel>
</rss>
