I don't know XML well enough to know what terms to use to ask the question. I'm going to obfuscate the XML snippet I'm trying to decode, hopefully not to the point of making the question unanswerable.
I have some XML, and I want to get the values "First String to Get" and "Second String to Get". How do I write an Oracle query to get them?
<itemTypes>
<itemTypesList>
<itemType>400SVFD2</itemType>
<ora:itemType xmlns:ora="http://www.oracle.com/something/somethingelse" navOpt="itemTypeMaint" searchZone="C1-ITEMTYPQ" tblKeyField="ITEM_TYPE_CD">First String to Get</ora:itemType>
<errorIfNoValue>C1NO</errorIfNoValue>
<valueType>U</valueType>
<valueSource>C1BF</valueSource>
<value/>
<billFactor>400SVFD2</billFactor>
<ora:billFactor xmlns:ora="http://www.oracle.com/something/somethingelse" navOpt="billFactorMaint" searchZone="C1-BFQ" tblKeyField="BF_CD">Second String to Get</ora:billFactor>
<valueAlgorithm/>
</itemTypesList>
If I write an XML query and put in the entire tag, what happens if some of those fields change? So I think I should be able to indicate just "ora:itemType" and "ora:billFactor", and essentially ignore the rest, right?
This returns null:
with test_table(xmldata) as (
select
q'[
<root>
<itemTypes>
<itemTypesList>
<itemType>400SVFD2</itemType>
<ora:itemType xmlns:ora="http://www.oracle.com/something/somethingelse" navOpt="itemTypeMaint" searchZone="C1-ITEMTYPQ" tblKeyField="ITEM_TYPE_CD">First String to Get</ora:itemType>
<errorIfNoValue>C1NO</errorIfNoValue>
<valueType>U</valueType>
<valueSource>C1BF</valueSource>
<value/>
<billFactor>400SVFD2</billFactor>
<ora:billFactor xmlns:ora="http://www.oracle.com/something/somethingelse" navOpt="billFactorMaint" searchZone="C1-BFQ" tblKeyField="BF_CD">Second String to Get</ora:billFactor>
<valueAlgorithm/>
</itemTypesList>
</itemTypes>
</root>
]'
from dual
)
select
xmlcast(
xmlquery(
'/root/itemTypes/itemTypesList/ora:itemType/text()'
passing xmltype(xmldata)
returning content
)
as varchar2(100)
) res
from test_table
;