I am unable to use xpath since i have read somewhere that 9i does not support namespaces. Is this true ? What is the workaround?
Thanks.
I asked Sean Dillon, our local XML guru, to take a look at this and here's what he said:
--
In Oracle9i release 1, you could not specify namespaces in XMLType functions. You can submit XPATH queries and you will get result whether or not they are in the default namespace or not. In release 2, this limitation has been lifted. Consider the following:
In a 9i release 1 database:
==================================================
SQL> connect sdillon/sdillon
Connected.
SQL> create table xml_documents(my_xml sys.xmltype);
Table created.
SQL> insert into xml_documents values (
2 sys.xmltype.createxml('<?xml version = ''1.0''?>
3 <ROWSET>
4 <ROW num="1">
5 <EMPNO>7369</EMPNO>
6 <ENAME>SMITH</ENAME>
7 <JOB>CLERK</JOB>
8 <MGR>7902</MGR>
9 <HIREDATE>12/17/1980 0:0:0</HIREDATE>
10 <SAL>800</SAL>
11 <DEPTNO>20</DEPTNO>
12 </ROW>
13 <ROW num="2">
14 <EMPNO>7499</EMPNO>
15 <ENAME>ALLEN</ENAME>
16 <JOB>SALESMAN</JOB>
17 <MGR>7698</MGR>
18 <HIREDATE>2/20/1981 0:0:0</HIREDATE>
19 <SAL>1600</SAL>
20 <COMM>300</COMM>
21 <DEPTNO>30</DEPTNO>
22 </ROW>
23 </ROWSET>'));
1 row created.
SQL> -- now an xml doc w/ a different namespace
SQL> insert into xml_documents values (
2 sys.xmltype.createxml('<?xml version = ''1.0''?>
3 <ROWSET xmlns="www.oracle.com/asktom.xml">
4 <ROW num="1">
5 <EMPNO>7369</EMPNO>
6 <ENAME>SMITH</ENAME>
7 <JOB>CLERK</JOB>
8 <MGR>7902</MGR>
9 <HIREDATE>12/17/1980 0:0:0</HIREDATE>
10 <SAL>800</SAL>
11 <DEPTNO>20</DEPTNO>
12 </ROW>
13 <ROW num="2">
14 <EMPNO>7499</EMPNO>
15 <ENAME>ALLEN</ENAME>
16 <JOB>SALESMAN</JOB>
17 <MGR>7698</MGR>
18 <HIREDATE>2/20/1981 0:0:0</HIREDATE>
19 <SAL>1600</SAL>
20 <COMM>300</COMM>
21 <DEPTNO>30</DEPTNO>
22 </ROW>
23 </ROWSET>'));
1 row created.
SQL> -- notice the second row of the result set has the fully qualified name:
SQL> select rownum, x.my_xml.extract('/ROWSET/ROW/EMPNO').getClobVal() empno
2 from xml_documents x;
ROWNUM EMPNO
---------- -----------------------------------------------------------------
1 <EMPNO>7369</EMPNO>
<EMPNO>7499</EMPNO>
2 <www.oracle.com/asktom.xml:EMPNO>7369</www.oracle.com/asktom.xml:EMPNO>
<www.oracle.com/asktom.xml:EMPNO>7499</www.oracle.com/asktom.xml:EMPNO>
==================================================
In a 9i release 2 database:
==================================================
SQL> connect sdillon/sdillon
Connected.
SQL> create table xml_documents(my_xml sys.xmltype);
Table created.
SQL> insert into xml_documents values (
2 sys.xmltype.createxml('<?xml version = ''1.0''?>
3 <ROWSET>
4 <ROW num="1">
5 <EMPNO>7369</EMPNO>
6 <ENAME>SMITH</ENAME>
7 <JOB>CLERK</JOB>
8 <MGR>7902</MGR>
9 <HIREDATE>12/17/1980 0:0:0</HIREDATE>
10 <SAL>800</SAL>
11 <DEPTNO>20</DEPTNO>
12 </ROW>
13 <ROW num="2">
14 <EMPNO>7499</EMPNO>
15 <ENAME>ALLEN</ENAME>
16 <JOB>SALESMAN</JOB>
17 <MGR>7698</MGR>
18 <HIREDATE>2/20/1981 0:0:0</HIREDATE>
19 <SAL>1600</SAL>
20 <COMM>300</COMM>
21 <DEPTNO>30</DEPTNO>
22 </ROW>
23 </ROWSET>'));
1 row created.
SQL> -- again, an xml document with a different namespace
SQL> insert into xml_documents values (
2 sys.xmltype.createxml('<?xml version = ''1.0''?>
3 <ROWSET xmlns="www.oracle.com/asktom.xml">
4 <ROW num="1">
5 <EMPNO>7369</EMPNO>
6 <ENAME>SMITH</ENAME>
7 <JOB>CLERK</JOB>
8 <MGR>7902</MGR>
9 <HIREDATE>12/17/1980 0:0:0</HIREDATE>
10 <SAL>800</SAL>
11 <DEPTNO>20</DEPTNO>
12 </ROW>
13 <ROW num="2">
14 <EMPNO>7499</EMPNO>
15 <ENAME>ALLEN</ENAME>
16 <JOB>SALESMAN</JOB>
17 <MGR>7698</MGR>
18 <HIREDATE>2/20/1981 0:0:0</HIREDATE>
19 <SAL>1600</SAL>
20 <COMM>300</COMM>
21 <DEPTNO>30</DEPTNO>
22 </ROW>
23 </ROWSET>'));
1 row created.
SQL> -- notice the second row has no data due to the namespace
SQL> select rownum, x.my_xml.extract('/ROWSET/ROW/EMPNO').getClobVal() empno
2 from xml_documents x;
ROWNUM EMPNO
---------- ----------------------------------------------------------------------
1 <EMPNO>7369</EMPNO>
<EMPNO>7499</EMPNO>
2
SQL> -- but now we can use a second parameter in the EXTRACT() function to
SQL> -- specify a namespace for our XPath expression:
SQL> select rownum,
2 x.my_xml.extract('/ROWSET/ROW/EMPNO',
3 'xmlns="www.oracle.com/asktom.xml"').getClobVal() empno
4 from xml_documents x;
ROWNUM EMPNO
---------- -------------------------------------------------------------------
1
2 <foo:EMPNO xmlns:foo="www.oracle.com/asktom.xml">7369</foo:EMPNO>
<foo:EMPNO xmlns:foo="www.oracle.com/asktom.xml">7499</foo:EMPNO>
This second parameter applies to all the 9i release 2 XMLType functions, such as EXTRACT(), EXTRACTVALUE(), EXISTSNODE(), and UPDATEXML().
There is namespace support THROUGHOUT the XML Developer's Kit as well, but considering your question the XMLType XPATH/namespace support was what you were interested in. For more information about namespace support in the Oracle XML Developer's Kits (XDKs), see the Oracle Technology Network (OTN) at </code>
http://otn.oracle.com/tech/xml/content.html
(You need an account to use OTN, but the accounts are free you just need to take the few minutes to register an account here:
http://otn.oracle.com/membership
Hope that helps!
____________________________________________________________________
Sean Dillon - Senior Technologist, Oracle Corporation
Author "Beginning Oracle Programming"
http://www.amazon.com/exec/obidos/ASIN/186100690X <code>