0

I create table in oracle 12c like this

CREATE TABLE Test_xml
    (
       xml_data                       SYS.XMLTYPE
    )

and insert XML data like this into column xml_data

<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns="http://XXXXX.XXX/schema/1.0" name="ABC">
  <person>
    <name1 value="AAAA" />
    <name2 value="CCCC" />
    <name3 value="XXXX" />
  </person>
  <person>
    <name1 value="11111" />
    <name2 value="22222" />
    <name3 value="33333" />
  </person>
</persons>

but when I try to select data out from table by query

SELECT xt.*
FROM   Test_xml x,
       XMLTABLE('/persons/person'
         PASSING x.xml_data
         COLUMNS 
           name1  VARCHAR2(2000) PATH 'name1/@value'
         ) xt;

not thing out from the query,How can i select name1 from the table?

2 Answers 2

2

You can provide the (default, in this case) namespace as another argument to XMLTable:

SELECT xt.*
FROM   Test_xml x,
       XMLTABLE(
         xmlnamespaces(default 'http://XXXXX.XXX/schema/1.0'),
         '/persons/person'
         PASSING x.xml_data
         COLUMNS 
           name1  VARCHAR2(2000) PATH 'name1/@value'
         ) xt;
| NAME1 |
| :---- |
| AAAA  |
| 11111 |

db<>fiddle showing your original query, @Sayan's wildcard version (which gets nulls for some reason, even though it works with a CTE), and this default namespace version.

Sign up to request clarification or add additional context in comments.

Comments

1

Just replace /persons/person to /*:persons/*:person

SELECT/*+ NO_XML_QUERY_REWRITE */ xt.*
FROM   Test_xml x,
       XMLTABLE('/*:persons/*:person'
         PASSING x.xml_data
         COLUMNS 
           name1  VARCHAR2(2000) PATH '*:name1/@value'
         ) xt;

Full example:

with test_xml(xml_data) as (
select xmltype(q'[<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns="http://XXXXX.XXX/schema/1.0" name="ABC">
  <person>
    <name1 value="AAAA" />
    <name2 value="CCCC" />
    <name3 value="XXXX" />
  </person>
  <person>
    <name1 value="11111" />
    <name2 value="22222" />
    <name3 value="33333" />
  </person>
</persons>]'
)
from dual
)
SELECT xt.*
FROM   Test_xml x,
       XMLTABLE('/*:persons/*:person'
         PASSING x.xml_data
         COLUMNS 
           name1  VARCHAR2(2000) PATH '*:name1/@value'
         ) xt;

NAME1
--------------------
AAAA
11111

14 Comments

I try your query It work when it select from dual but if apply to the table, the result of name1 is null but return correct number of row in table. Am I missing something?
@maxzzxam did you notice *:name1/@value?
@SayanMalakshinov - I included your wildcard version in my db<>fiddle; and it doesn't work from a table. Not sure why, or why it would behave differently. Dumping the XML shows the same data type (58) but different lengths.
@AlexPoole huh! You found a bug in columns clause :) I've included another variant into you db<>fiddle: dbfiddle.uk/…
Re "different lengths": Though that's not ok, but it's understandable: oracle by default uses binary xml storage since 11.2 afair
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.