Search notes:

Oracle SQL functions: XMLQuery

XMLQuery ( XQueryExpression [ XML-passing-clause ] RETURNING CONTENT [ NULL ON EMPTY ]
The returned data type is XMLType.
select
   XMLQuery('
     <nums>
     {
        for $i in (1,1,2,3,5,8,13) return <num>{ $i }</num>
     }
     </nums>
   ' returning content).extract('//num[6]/text()').getStringVal() sixth_fibonacci_number
from
   dual;
The following statement returns <e1>hello world</e1> (including the tags!):
select
   xmlQuery('//e1' passing xmlType('<X><e1>hello world</e1><e2>42</e2></X>') returning content).getStringVal()
from
   dual;

Arithmetical expressions

The following query returns 6:
drop table tq84_xml;

create table tq84_xml (
  id  number,
  val clob
);

insert into tq84_xml values (1, '<nums>
  <n>1</n><t>one</t>
  <n>1</n><t>one</t>
  <n>2</n><t>two</t>
  <n>3</n><t>three</t>
  <n>5</n><t>five</t>
  <n>8</n><t>eight</t>
</nums>');


select
   xmlQuery(
   '
      for $n in /nums/n 
         return <n-times-3> { 
           3 * xs:integer( $n ) (: use xs:integer to cast element value to appropriate type :)
        } </n-times-3>'
      passing xmlType(tq84_xml.val)
      returning content
   )
   as x
from
   tq84_xml;
This query returns
<n-times-3>3</n-times-3><n-times-3>3</n-times-3><n-times-3>6</n-times-3><n-times-3>9</n-times-3><n-times-3>15</n-times-3><n-times-3>24</n-times-3>

Accessing data in tables and views

With fn:collection("oradb:/user-name/table-or-view-name") it is possible to access data in tables and views:
select XMLQuery(
   'for $obj
          in fn:collection("oradb:/SYS/USER_OBJECTS")
        where
           $obj/ROW/OBJECT_TYPE ne "LOB"
        return
          <obj>
            <name>{ $obj/ROW/OBJECT_NAME/text() } </name>
            <type>{ $obj/ROW/OBJECT_TYPE/text() } </type>
        </obj>
   '
   returning content)
   as objects
from dual;

See also

The functionality of XMLQuery (and XMLCast) obsolete the deprecated functions extract and extractValue.
XML related SQL functions

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758207108, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/SQL/functions/XML/Query/index(119): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78