weislw
asked on
How to query and extract a specific XML tag value from Oracle CLOB field containing XML data
I have an Oracle table with a CLOB field that contains XML-Data. I would like query this table and make use of Oracles xmltype function and extract the value of a specific XML-Tag.
Oracle returns me all subelements of the root node. When I try to extract values of nodes further down in the xml-tree hierarchy I get no result.
I would be grateful if you could help me.
Best regards,
Wil
Table definition:
CREATE TABLE user.XML_TABLE
(
XML_DATA CLOB
)
Example clob field contents (field xml_data):
<xml-fragment client="muster_max" offerId="0000900513000000" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<product productId="PROD-ID" code="AB-CD" type="TYPE_X" xmlns="http://www.something.com/is/xml/offer">Product Name</product>
.... further tags ...
</xml-fragment>
Queries:
1) select xmltype(xml_data) from XML_TABLE;
select extract(xmltype(xml_data), '/xml-frag ment') from XML_TABLE;
Both queries return the whole contents of the field xml_data
2) select extract(xmltype(xml_data), '/xml-frag ment/*') from XML_TABLE
The query returns all subelements of tag <xml-fragment></xml-fragme nt>
3) select extract(xmltype(xml_data), '/xml-frag ment/produ ct/*') from XML_TABLE
The query returns nothing. I would expect to receive all subelements of node product.
4) select extract(xmltype(xml_data), '/xml-frag ment/produ ct/text()' ) from XML_TABLE
The query returns nothing. I would expect to receive the value 'Product name'
Oracle returns me all subelements of the root node. When I try to extract values of nodes further down in the xml-tree hierarchy I get no result.
I would be grateful if you could help me.
Best regards,
Wil
Table definition:
CREATE TABLE user.XML_TABLE
(
XML_DATA CLOB
)
Example clob field contents (field xml_data):
<xml-fragment client="muster_max" offerId="0000900513000000"
<product productId="PROD-ID" code="AB-CD" type="TYPE_X" xmlns="http://www.something.com/is/xml/offer">Product Name</product>
.... further tags ...
</xml-fragment>
Queries:
1) select xmltype(xml_data) from XML_TABLE;
select extract(xmltype(xml_data),
Both queries return the whole contents of the field xml_data
2) select extract(xmltype(xml_data),
The query returns all subelements of tag <xml-fragment></xml-fragme
3) select extract(xmltype(xml_data),
The query returns nothing. I would expect to receive all subelements of node product.
4) select extract(xmltype(xml_data),
The query returns nothing. I would expect to receive the value 'Product name'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the quick reply. I'll give it a try.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the example. It helped.
glad we could help
All I can find is examples of "select extract" that don't include namespaces
After that they suggest to use full XQuery.
That makes sence, but if you are comfortable with XPath and "select extract", you can use my example to get it done
(it is slightly less efficient though)