SELECT from XML to get multiple rows
177437Aug 30 2009 — edited Aug 31 2009We are trying to set up web services to facilitate access to a central asset database. I’m able to make the request, & I store the response in the following table:
CREATE TABLE assets
(asset_name VARCHAR2(40),
asset_xml XMLType,
add_dt DATE);
Different asset types can have the same name so when I get a response there may be several assets returned with the same name but different types as seen in this formatted XML:
<soapenv:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<AssetDetailsResponse>
<Asset>
<ID>44535</ID>
<AssetName>asset1</AssetName>
<TypeName>Server</TypeName>
<Status>Active</Status>
<SerialNumber>jk823</SerialNumber>
<Business>Finance</Business>
<Vendor>hewlett packard</Vendor>
<Location>
<CountryName>United States</CountryName>
<StateProvName>Ohio</StateProvName>
<CityTownName>Cincinnati</CityTownName>
<Street>2875 Vine Street</Street>
<PostalCode>45229</PostalCode>
</Location>
</Asset>
<Asset>
<ID>453051</ID>
<AssetName>asset1</AssetName>
<TypeName>Tape Backup</TypeName>
<Status>Inactive</Status>
<SerialNumber>s97032</SerialNumber>
<Business>Sales</Business>
<Vendor>Any vendor</Vendor>
<Location>
<CountryName>United States</CountryName>
<StateProvName>Ohio</StateProvName>
<Street>7683 Main Street</Street>
<CityTownName>Cincinnati</CityTownName>
<PostalCode>45492</PostalCode>
</Location>
</Asset>
</AssetDetailsResponse>
</soapenv:Body>
</soapenv:Envelope>
The following query works but the assets are strung together rather than on separate rows:
SQL> SELECT extract(asset_xml, '//AssetName/text()') from gewscadb;
EXTRACT(ASSET_XML,'//ASSETNAME/TEXT()')
-----------------------------------------------------------------------
asset1asset1
I have tried the following queries to return separate rows but they return “no rows selected”:
SELECT a.asset_id, a.asset_name
FROM assets,
XMLTABLE('/Asset' PASSING assets.asset_xml
COLUMNS asset_id VARCHAR2(20) PATH '/Asset/ID',
asset_name VARCHAR2(30) PATH '/Asset/AssetName') a;
SELECT a.asset_id, a.asset_name
FROM assets,
XMLTABLE('/AssetDetailsResponse'
PASSING assets.asset_xml
COLUMNS asset_id VARCHAR2(20) PATH '/AssetDetailsResponse/Asset/ID',
asset_name VARCHAR2(30) PATH '/AssetDetailsResponse/Asset/AssetName') a;
I suspect there is a problem with my path names in these queries. Can someone please provide some guidance on how I can get a separate row for each asset?