/**
 - $Id$
 */

-----------------------------------------------------------
-- BULKLOAD
-----------------------------------------------------------
cd ./pgxload
make clean;make
./pgxload -u postgres -p passwd -d xpsql ../test/test01.xml


-----------------------------------------------------------
-- DOM write
-----------------------------------------------------------

/**
 create new document.
 */
xpsql=# select create_new_document ('xpsql.xml');
--
2
(1 row)

/**
 create child (root) element.
 new_childelment function is createElement + appendChild in DOM.
 */
xpsql=# select new_childelement(1,'top');
--
3
(1 row)

/**
 create element under element "top" in DOM process.
 */
/* create element */
xpsql=# select createelement(1,'second');
--                          ~~~
4                          root-node id
(1 row)

-- node number is a substibution of reference.

/* append child */
xpsql=# select appendChild(3,4);
--
t
(1 row)

xpsql=# select new_childelement(3,'third');
--
5
(1 row)

/**
 set attribute to element "third"
 */
xpsql=# select setAttribute(5,'attname','attval');
--
7
(1 row)


xpsql=# select toXML(id) from xpath_eval('/top');
                                         toxml
----------------------------------------------------------------------------------------
 <?xml version="1.0" encoding="EUC-JP"?>
<top><second/><third attname="attval"/></top>

(1 row)


-----------------------------------------------------------
-- DOM retrieve
-----------------------------------------------------------

/* getDocumentElement */
select getDocumentElement(getDocument('xpsql.xml'));
--
3
(1 row)

xpsql=# select getFirstChild(3);
--
4
(1 row)

xpsql=# select getElementsByTagname(3,'third');
--
5
(1 row)


-----------------------------------------------------------
-- convert XPath to SQL
-----------------------------------------------------------
select xpath2sql('/site/open_auctions/open_auction');

-- above query is equals to
select xpath2sql('/site/open_auctions/open_auction',0);

-- return SQL that extracts ordered flagment
select xpath2sql('/site/open_auctions/open_auction',3);


-----------------------------------------------------------
-- evaluation xpath
-----------------------------------------------------------
select * from xpath_eval('/site/open_auctions/open_auction');
select * from xpath_eval('/site/open_auctions/open_auction',3);

-----------------------------------------------------------
-- evaluation xpath and retrieve with xml format
-----------------------------------------------------------
select toXML(id) from xpath_eval('/top');

xpsql=# select toXML(id) from xpath_eval('/top');
--
 <?xml version="1.0" encoding="EUC-JP"?>
<top><second/><third attname="attval"/></top>

(1 row)


-----------------------------------------------------------
-- evaluation '/PLAY' and set selected node-content "hoge"
-----------------------------------------------------------
UPDATE
    xml_node
SET
    content = 'hoge'
FROM
    xpath_eval('/PLAY') as Q
WHERE
    xml_node.id = Q.id;