patternMinor
XMLDB - Nodes to Rows
Viewed 0 times
rowsnodesxmldb
Problem
I have an application that stores XML as XMLType in a table. This is the first time I'm using XMLDB and XMLType features, and was wondering if I could use the results from an XQuery FLWOR expression in a "relational" way.
Considering the following example XML:
You'll probably notice that these are extracts from the Windows Event Log.
These results (the actual XML files are, of course, way bigger and contain a lot of other metrics, but the principle remains the same) are stored in following table:
I can use XQuery to, for example, query all the Provider names of an Event Record in the different XML documents, like this:
And the results would be along these lines:
However, the results I would like to have is this:
Or, when querying multiple documents:
```
1 5d4a9a3f-5a
Considering the following example XML:
Faulting application name: wmiprvse.exe
Faulting application name: wmiprvse.exe
A thread in process C:\Program Files\Common Files\McAfee\SystemCore\mcshield.exe took longer than 90000 ms to complete
You'll probably notice that these are extracts from the Windows Event Log.
These results (the actual XML files are, of course, way bigger and contain a lot of other metrics, but the principle remains the same) are stored in following table:
CREATE TABLE SPERF_XML_RAW
(
"ID" NUMBER NOT NULL,
"GUID" CHAR(36 CHAR) NOT NULL,
"HOSTNAME" VARCHAR2(16 CHAR) NOT NULL,
"USERNAME" VARCHAR2(16 CHAR) NOT NULL,
"DATA_XML" PUBLIC.XMLTYPE NOT NULL,
"DATE_CREATED" TIMESTAMP(6) NOT NULL,
"DATE_INSERTED" TIMESTAMP(6) DEFAULT SYSDATE NOT NULL,
CONSTRAINT SPERF_XML_RAW_PK PRIMARY KEY(ID)
)I can use XQuery to, for example, query all the Provider names of an Event Record in the different XML documents, like this:
SELECT ID, GUID,
XMLQuery(
'for $i in /PerfInfo/EventLog/EventRecord
return $i/@Provider'
PASSING DATA_XML RETURNING CONTENT
) "DATA_THINGS"
FROM SPERF_XML_RAW;And the results would be along these lines:
1 5d4a9a3f-5a1c-478c-b12f-39e9f33172f4 Application ErrorApplication ErrorMcLogEventHowever, the results I would like to have is this:
1 5d4a9a3f-5a1c-478c-b12f-39e9f33172f4 Application Error
1 5d4a9a3f-5a1c-478c-b12f-39e9f33172f4 Application Error
1 5d4a9a3f-5a1c-478c-b12f-39e9f33172f4 McLogEventOr, when querying multiple documents:
```
1 5d4a9a3f-5a
Solution
I found the answer myself after reading through loads of documentation from Oracle. The answer comes in two parts.
First, my original question can be solved by using XMLTable:
Note that COLUMN_VALUE is a pseudocolumn that can be used in conjunction with XMLTable
As additional information I would like to add the following. You can create multiple columns with the following syntax:
But Oracle will throw ORA-19279: XPTY0004 when one or more columns return multiple nodes.
The solution to THAT problem can be found below:
This will probably fail once more as soon as your document has too many levels, and I have not included a document with namespaces, which will most likely cause this to explode as well.
All information was found in the XML DB Developers guide: https://docs.oracle.com/cd/B28359_01/appdev.111/b28369/xdb03usg.htm#g1055369
First, my original question can be solved by using XMLTable:
SELECT
x.ID AS "MEASUREMENT_ID",
x.GUID AS "MEASUREMENT_GUID",
a.COLUMN_VALUE AS "MESSAGE"
FROM SPERF_XML_RAW x,
XMLTable('/PerfInfo/EventLog/EventRecord/@Provider'
PASSING x.DATA_XML) a;Note that COLUMN_VALUE is a pseudocolumn that can be used in conjunction with XMLTable
As additional information I would like to add the following. You can create multiple columns with the following syntax:
SELECT
x.ID,
x.GUID,
res."Provider",
res."TimeCreated"
FROM SPERF_XML_RAW x,
XMLTABLE('/PerfInfo'
PASSING x.DATA_XML
COLUMNS
"Provider" VARCHAR2(32 CHAR) PATH '/PerfInfo/EventLog/EventRecord',
"TimeCreated" TIMESTAMP PATH '/PerfInfo/EventLog/EventRecord') res;But Oracle will throw ORA-19279: XPTY0004 when one or more columns return multiple nodes.
ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
19279. 00000 - "XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence"
*Cause: The XQuery sequence passed in had more than one item.
*Action: Correct the XQuery expression to return a single item sequence.The solution to THAT problem can be found below:
SELECT
x.ID AS "MEASUREMENT_ID",
x.GUID AS "MEASUREMENT_GUID",
res."TIMECREATED",
res."LOGNAME",
res."LEVEL",
res."ID",
res."PROVIDER",
res."MESSAGE"
FROM SPERF_XML_RAW x,
XMLTable('/PerfInfo'
PASSING x.DATA_XML
COLUMNS
"EventRecords" XMLTYPE PATH 'EventLog') rec,
XMLTABLE ('EventLog/EventRecord'
PASSING rec."EventRecords"
COLUMNS
"LOGNAME" VARCHAR(32 CHAR) PATH '@LogName',
"ID" NUMBER PATH '@ID',
"LEVEL" CHAR(1 CHAR) PATH '@Level',
"PROVIDER" VARCHAR2(128 CHAR) PATH '@Provider',
"TIMECREATED" VARCHAR2(32 CHAR) PATH '@TimeCreated',
"MESSAGE" CLOB PATH 'text()') res;This will probably fail once more as soon as your document has too many levels, and I have not included a document with namespaces, which will most likely cause this to explode as well.
All information was found in the XML DB Developers guide: https://docs.oracle.com/cd/B28359_01/appdev.111/b28369/xdb03usg.htm#g1055369
Code Snippets
SELECT
x.ID AS "MEASUREMENT_ID",
x.GUID AS "MEASUREMENT_GUID",
a.COLUMN_VALUE AS "MESSAGE"
FROM SPERF_XML_RAW x,
XMLTable('/PerfInfo/EventLog/EventRecord/@Provider'
PASSING x.DATA_XML) a;SELECT
x.ID,
x.GUID,
res."Provider",
res."TimeCreated"
FROM SPERF_XML_RAW x,
XMLTABLE('/PerfInfo'
PASSING x.DATA_XML
COLUMNS
"Provider" VARCHAR2(32 CHAR) PATH '/PerfInfo/EventLog/EventRecord',
"TimeCreated" TIMESTAMP PATH '/PerfInfo/EventLog/EventRecord') res;ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
19279. 00000 - "XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence"
*Cause: The XQuery sequence passed in had more than one item.
*Action: Correct the XQuery expression to return a single item sequence.SELECT
x.ID AS "MEASUREMENT_ID",
x.GUID AS "MEASUREMENT_GUID",
res."TIMECREATED",
res."LOGNAME",
res."LEVEL",
res."ID",
res."PROVIDER",
res."MESSAGE"
FROM SPERF_XML_RAW x,
XMLTable('/PerfInfo'
PASSING x.DATA_XML
COLUMNS
"EventRecords" XMLTYPE PATH 'EventLog') rec,
XMLTABLE ('EventLog/EventRecord'
PASSING rec."EventRecords"
COLUMNS
"LOGNAME" VARCHAR(32 CHAR) PATH '@LogName',
"ID" NUMBER PATH '@ID',
"LEVEL" CHAR(1 CHAR) PATH '@Level',
"PROVIDER" VARCHAR2(128 CHAR) PATH '@Provider',
"TIMECREATED" VARCHAR2(32 CHAR) PATH '@TimeCreated',
"MESSAGE" CLOB PATH 'text()') res;Context
StackExchange Database Administrators Q#83418, answer score: 4
Revisions (0)
No revisions yet.