Search code examples
sqloracle-databasexqueryxmltypexmltable

Oracle 11g: XQuery results concatenated in a single SQLXML type


When I run an XQuery query in PL-SQL using

 SELECT XQUERY('...' RETURNING CONTENT) FROM DUAL

the result is always returned concatenated in a single row single column holding a SQLXML field.

This is bad for queries returning sequences of elements and really nasty for queries returning sequences of text nodes.

Is there a way to avoid this concatenation and get a row per returned item?

Thanks,

Eric


Solution

  • To elaborate on jonearles' answer, XMLTable is as generic as XQUERY, the difference being that XQUERY returns a single value while XMLTable is seen as full table.

    With XMLTable, you can do things such as:

    SQL> select * from XMLTABLE ('declare variable $v as xs:string external; $v, $v' PASSING 'Hello World' as "v") ;
    
    COLUMN_VALUE
    --------------------------------------------------------------------------------
    Hello World
    Hello World
    

    and also:

    SQL> select * from XMLTABLE ('declare variable $v as xs:string external; <e>{$v}</e>,<e>{$v}</e>' PASSING 'Hello World' as "v") ;
    
    COLUMN_VALUE
    --------------------------------------------------------------------------------   
    <e>Hello World</e>
    <e>Hello World</e>
    

    and that does exactly what I was looking for.