2

I am trying to extract a value from an xml element, located in an XMLTYPE column in an Oracle Table. The xml element which I am trying to extract have a parent for which a namespace is defined. The xml looks something like:

<a>
  <b xmlns="urn:www.someSite.com/myModel">
    <c>my value</c>
  </b>
</a>

If I want to extract the content of the "a" element, its context is correctly returned:

SELECT Extract(myColumn, '/a') FROM myTable;

But for returning the content of the "c" element I didn't succeed to find any version to work. The following instructions does not work:

SELECT Extract(myColumn, '/a/b/c') FROM myTable;

SELECT Extract(myColumn, '/a/b/c', 'xmlns="urn:www.someSite.com/myModel"') FROM myTable;

SELECT Extract(myColumn, '/a/b/c', 'urn:www.someSite.com/myModel') FROM myTable;

Can anybody help me, with the extract statement that would work in this case?

axl g
  • 612
  • 2
  • 9
  • 20

4 Answers4

5
select a.*
from   XMLTABLE(
         XMLNAMESPACES('urn:www.someSite.com/myModel' AS "ns"),
         '/*'
         PASSING my.myColumn           
         COLUMNS
           val  VARCHAR2(2000)   PATH '/a/ns:b/ns:c'
       ) a, myTable my;
Jokke Heikkilä
  • 918
  • 1
  • 8
  • 17
  • I will add that this is also the better modern solution now since extract/extractvalue are depreciated. – REW Jun 08 '15 at 20:16
4

Since the a element does not have the namespace, you can first extract its child elements without using namespaces in the function, and then extract the value from the b with the namespace:
Try:

select extract(extract(myColumn, 'a/*'),
               'b/c/text()',
               'xmlns=urn:www.someSite.com/myModel') 
  from myTable
A.B.Cade
  • 16,735
  • 1
  • 37
  • 53
0

When the default namespace changes, one way to specify namespaces is to write the wildcard character ('*') and the local-name() and namespace-uri() XPath functions.

select extract(myColumn, '/a/*[local-name()='b' and namespace-uri()='urn:www.someSite.com/myModel']/*[local-name()='c' and namespace-uri()='urn:www.someSite.com/myModel']') from myTable

-1
SELECT XMLAGG(XMLELEMENT(E,ename||',')).EXTRACT('//text()') "Result"
FROM   emp
Mureinik
  • 297,002
  • 52
  • 306
  • 350
sony
  • 1