I am wondering how I can insert an XML file into a SQL Server DB. Below is the XML I have but I am unsure how to do this in a way that will scale. My thought is a Insert Into Select statement but I do not know if that is going to work as the data increases. Thank you in advance!
<Records>
<Record>
<ID SpecNum="5069580" IssueNum="001" SpecStatus="Pre-Approved">
<NutritionDetails>
<NutrientFacts>
<NutrientNameId>ENERC_KCAL</NutrientNameId>
<NutrientName>ENERC_KCAL</NutrientName>
<NutrientPer100gUnrounded>1.91</NutrientPer100gUnrounded>
<NutrientPer100gRounded>191</NutrientPer100gRounded>
</NutrientFacts>
</NutritionDetails>
</ID>
</Record>
</Records>
Once you've successfully created a proper, valid XML - you should be able to use this T-SQL code to grab the details:
SELECT
-- get the attributes from the <ID> node
IDSpecNum = XC.value('(ID/#SpecNum)[1]', 'int'),
IDIsseNum = XC.value('(ID/#IssueNum)[1]', 'int'),
IDSpecStatus = XC.value('(ID/#SpecStatus)[1]', 'varchar(100)'),
-- get the element values from the children of the <NutrientFacts> node
NutrientNameId = NUT.value('(NutrientNameId)[1]', 'varchar(100)'),
NutrientName = NUT.value('(NutrientName)[1]', 'varchar(100)'),
NutrientPer100gUnrounded = NUT.value('(NutrientPer100gUnrounded)[1]', 'decimal(20,4)'),
NutrientPer100gRounded = NUT.value('(NutrientPer100gRounded)[1]', 'decimal(20,4)')
FROM
dbo.YourTable
CROSS APPLY
-- get one XML fragment per <Record>
XmlData.nodes('/Records/Record') AS XT(XC)
CROSS APPLY
-- get one XML fragment per <NutrientFacts> inside
XC.nodes('ID/NutritionDetails/NutrientFacts') AS XT2(NUT)
The first CROSS APPLY basically get an "inline pseudo table" with one XML fragment for each <Record> node in your XML in the XmlData column of your table (this is just an assumption on my part - adapt to your reality!). These XML fragments are referenced as "pseudo-table" XT with a single column XC.
With that XC column's XML fragment, you can "reach in" and grab the attribute values from the <ID> node in the <Record> - that's the first three values.
Then, based on the XT pseudo table, I apply another CROSS APPLY to get all the <NutrientFacts> nodes inside <ID> / <NutritionDetails> - those are referenced as pseudo-table XT2 with column NUT, which again holds an XML fragment for each <NutrientFacts> node; I reach into that XML node and extract the values from the sub-elements of that node - those are the four additional values that are shown in the select.
Now that you have a SELECT that returns all the values - you can easily get those bits you need and use them in a INSERT INTO dbo.MyTable(list-of-columns) SELECT list-of-columns :...... scenario. Enjoy!
UPDATE: to import an XML file from disk (local disk on your SQL Server machine's file system) into your table - use something like this:
INSERT INTO dbo.YourTable(XmlData)
SELECT
CONVERT(XML, BulkColumn) AS BulkColumn
FROM
OPENROWSET(BULK 'C:\temp\records.xml', SINGLE_BLOB) AS x;
Again: adapt to your needs - I don't know if you want to insert additional information into dbo.YourTable - and I don't even know your table name; you can load one XML at a time from disk
Related
I have an XML column in a table which i am trying to parse out values from to flat table structure.
I am trying to input the XML here but stackoverflow ses it as code and when i try and format as code it still won't accept it.
I can't even get data from "Header" level.
<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Message.xsd">
<Header>
<Verb>created</Verb>
<Noun>MeterReadings</Noun>
<Timestamp>2021-03-08T00:57:18+01:00</Timestamp>
<Source>Ipsum Lorum</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<AckRequired>true</AckRequired>
<MessageID>Ipsum Lorum</MessageID>
<CorrelationID />
</Header>
<Payload>
<MeterReadings xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#" xmlns="http://iec.ch/TC57/2011/MeterReadings#">
<MeterReading>
<IntervalBlocks>
<IntervalReadings>
<timeStamp>2021-03-07T01:00:00+01:00</timeStamp>
<value>480.196</value>
<ReadingQualities>
<ReadingQualityType ref="3.0.0" />
</ReadingQualities>
</IntervalReadings>
<IntervalReadings>
<ReadingType ref="11.0.7.3.1.2.12.1.1.0.0.0.0.101.0.3.72.0" />
</IntervalReadings>
</IntervalBlocks>
<Meter>
<mRID>0000000000000</mRID>
<status>
<remark>Ipsum Lorum</remark>
<value>ESP</value>
</status>
</Meter>
<UsagePoint>
<mRID>73599900000000</mRID>
</UsagePoint>
</MeterReading>
</MeterReadings>
</Payload>
</RequestMessage>
I am not able to parse it and i have tried using examples from other threads. I am trying to not use OPENXML solution because requires DECLARE and executing the built in procedure for clearing the XML from memmory periodically. I am trying to use the OUTER APPLY solution.
Like Shugos solution in How to parse XML data in SQL server table or Query XML with nested nodes on Cross Apply.
It doesn't work.
It returns null for the timestamp column.
select
t.file_created_time
,c.value('(Timestamp)[1]','varchar(max)') as timestamp
from load.t t
OUTER APPLY t.xml_data.nodes('RequestMessage/Header') as m(c)
Please try the following solution.
Starting from SQL Server 2005 onwards, it is better to use XQuery language, based on the w3c standards, while dealing with the XML data type.
Microsoft proprietary OPENXML and its companions sp_xml_preparedocument and sp_xml_removedocument are kept just for backward compatibility with the obsolete SQL Server 2000. Their use is diminished just to very few fringe cases.
I had to comment out the following tag <!--<IntervalReadings>--> to make your XML well-formed.
XML Header fragment has a default namespace:
xmlns="http://iec.ch/TC57/2011/schema/message"
XML Payload fragment has its own two additional namespaces:
xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#"
xmlns="http://iec.ch/TC57/2011/MeterReadings#"
Namespaces should be taken into account.
Check it out below.
SQL
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO #tbl (xml_data) VALUES
(N'<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Message.xsd">
<Header>
<Verb>created</Verb>
<Noun>MeterReadings</Noun>
<Timestamp>2021-03-08T00:57:18+01:00</Timestamp>
<Source>Ipsum Lorum</Source>
<AsyncReplyFlag>true</AsyncReplyFlag>
<AckRequired>true</AckRequired>
<MessageID>Ipsum Lorum</MessageID>
<CorrelationID/>
</Header>
<Payload>
<MeterReadings xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#"
xmlns="http://iec.ch/TC57/2011/MeterReadings#">
<MeterReading>
<IntervalBlocks>
<IntervalReadings>
<timeStamp>2021-03-07T01:00:00+01:00</timeStamp>
<value>480.196</value>
<ReadingQualities>
<ReadingQualityType ref="3.0.0"/>
</ReadingQualities>
</IntervalReadings>
<!--<IntervalReadings>-->
<ReadingType ref="11.0.7.3.1.2.12.1.1.0.0.0.0.101.0.3.72.0"/>
</IntervalBlocks>
<Meter>
<mRID>0000000000000</mRID>
<status>
<remark>Ipsum Lorum</remark>
<value>ESP</value>
</status>
</Meter>
<UsagePoint>
<mRID>73599900000000</mRID>
</UsagePoint>
</MeterReading>
</MeterReadings>
</Payload>
</RequestMessage>');
-- DDL and sample data population, end
WITH XMLNAMESPACES(DEFAULT 'http://iec.ch/TC57/2011/schema/message')
SELECT id
, c.value('(Noun/text())[1]','VARCHAR(30)') AS Noun
, c.value('(Timestamp/text())[1]','DATETIMEOFFSET(0)') AS [timestamp]
FROM #tbl
CROSS APPLY xml_data.nodes('/RequestMessage/Header') AS t(c);
Output
+----+---------------+----------------------------+
| id | Noun | timestamp |
+----+---------------+----------------------------+
| 1 | MeterReadings | 2021-03-08 00:57:18 +01:00 |
+----+---------------+----------------------------+
You need to respect and include the XML namespace in your XML document in your XQuery!
<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message"
**********************************************
Try something like this:
WITH XMLNAMESPACES(DEFAULT N'http://iec.ch/TC57/2011/schema/message')
SELECT
t.id,
c.value('(Timestamp)[1]','varchar(max)') as timestamp
FROM
load.t t
CROSS APPLY
t.xml_data.nodes('RequestMessage/Header') AS m(c)
Also when trying to run this on my SQL Server, I get an error that the XML as shown is malformed.....
UPDATE:
If you need to also access bits in the Payload section - you need to also respect that XML namespace there:
<MeterReadings xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#"
xmlns="http://iec.ch/TC57/2011/MeterReadings#">
***********************************************
Try this:
WITH XMLNAMESPACES(N'http://iec.ch/TC57/2011/schema/message' as hdr,
N'http://iec.ch/TC57/2011/MeterReadings#' as mr)
SELECT
t.id,
c.value('(hdr:Timestamp)[1]', 'varchar(50)') AS timestamp,
col.value('(mr:MeterReading/mr:IntervalBlocks/mr:IntervalReadings/mr:timeStamp)[1]', 'varchar(50)') AS MeterReadingsTimestamp
FROM
load.t t
CROSS APPLY
t.xml_data.nodes('/hdr:RequestMessage/hdr:Header') AS m(c)
CROSS APPLY
t.xml_data.nodes('/hdr:RequestMessage/hdr:Payload/mr:MeterReadings') AS mr(col)
I'm trying to flatten XML data in a SQL query but I always seem to get nulls.
I tried the cross/outer apply method described here.
The column with XML data is called Data.
I'm guessing that the xml data with these links need to be somehow also added?
Could you please help to get a proper SQL query?
Query I tried:
SELECT
v.name
,pref.value('(LocalId/text())[1]', 'nvarchar(10)') as localid
FROM [database].[requests] v
outer apply v.Data.nodes('/DataForm') x(pref)
GO
example of xml data in that column:
<Dataform xmlns="http://somelongasslink.org/hasalsosomestuffhere" xmlns:i="http://somexlmschemalink/">
<DeleteDate xmlns="http://somelongasslink.org/hasalsosomestuffhere" i:nil="true" />
<LocalId xmlns="http://somelongasslink.org/hasalsosomestuffhere">5325325</LocalId>
...
You can use this code to get the result you're looking for:
;WITH XMLNAMESPACES(DEFAULT 'http://somelongasslink.org/hasalsosomestuffhere')
SELECT
rq.Name,
LocalID = TC.value('(LocalId)[1]', 'nvarchar(10)')
FROM
[database].[requests] rq
CROSS APPLY
rq.Data.nodes('/Dataform') AS TX(TC)
GO
There were two problems with your code:
you're not respecting / including the XML namespace that's defined on the XML document
<Dataform xmlns="http://somelongasslink.org/hasalsosomestuffhere"
*******************************************************
you didn't pay attention to the case-sensitivity of XML in your call to .nodes() - you need to use .nodes('/Dataform') (not /DataForm - the F is not capitalized in your XML)
I'm trying to parse an XML file that I get from a url (sample below) and I need to parse the items that are on the record node. I am planning to put it into a SQL database with code at the bottom but can't figure out that line of code
<pmcids status="ok">
<request idtype="pmid" pmids="" versions="yes" showaiid="no">
<echo>ids=19240239;tool=HCC;email=morgenxxx%40xxxx.edu;format=xml</echo>
</request>
<record requested-id="19240239" pmcid="PMC2668929" pmid="19240239" doi="10.1158/1055-9965.EPI-08-0866">
<versions><version pmcid="PMC2668929.1" mid="NIHMS104698" current="true"/>
</versions>
</record>
</pmcids>
SQL code:
nref.value('#PMID[1]','varchar(max)') pmid,
nref.value('#PMCID[1]','varchar(max)') PMCID
All help is appreciated. I hope that this is enough information to determine the correct syntax
Use the native XQuery support in SQL Server! Much simpler than OPENXML ....
Try this:
DECLARE #input XML = '<pmcids status="ok">
<request idtype="pmid" pmids="" versions="yes" showaiid="no">
<echo>ids=19240239;tool=HCC;email=morgenxxx%40xxxx.edu;format=xml</echo>
</request>
<record requested-id="19240239" pmcid="PMC2668929" pmid="19240239" doi="10.1158/1055-9965.EPI-08-0866">
<versions>
<version pmcid="PMC2668929.1" mid="NIHMS104698" current="true"/>
</versions>
</record>
</pmcids>'
SELECT
RequestedId = xc.value('#requested-id', 'int'),
pmcid = xc.value('#pmcid', 'varchar(50)'),
pmid = xc.value('#pmid', 'int'),
doi = xc.value('#doi', 'varchar(50)')
FROM
#input.nodes('/pmcids/record') AS XT(XC)
Basically, the .nodes() call returns a "virtual" table XT with a column XC that contains the XML fragment for each of the XML nodes that match your XPath expression - here a list of all <record> nodes under the <pmcids> root node.
Then, using the .value() call, you can "reach into" each of those nodes in the XML elements and retrieve the individual bits - since those are all attributes, you the # prefix to indicate an attribute, and define the data type of your attribute.
This gives me an output of:
which you could easily insert into a database table
Update: if you also need the mid from the <version> node - use this:
SELECT
RequestedId = xc.value('#requested-id', 'int'),
pmcid = xc.value('#pmcid', 'varchar(50)'),
pmid = xc.value('#pmid', 'int'),
doi = xc.value('#doi', 'varchar(50)'),
VersionPmcid = xver.value('#pmcid', 'varchar(50)'),
mid = xver.value('#mid', 'varchar(50)')
FROM
#input.nodes('/pmcids/record') AS XT(XC)
CROSS APPLY
XC.nodes('versions/version') AS XT2(XVer)
(I added the pmcid attribute from the <version> node, since there might be multiple <version> nodes under a <record> from what this sample looks like)
I have a XML in Source Table. I need to parse this XML to 3 different tables which has Parent Child relationship. I can do this in C# but currently for this i need to implement it at SQL server side.
The sample xml looks like:
<ROWSET>
<ROW>
<HEADER_ID>5001507</HEADER_ID>
<ORDER_NUMBER>42678548</ORDER_NUMBER>
<CUST_PO_NUMBER>LSWQWE1</CUST_PO_NUMBER>
<CUSTOMER_NUMBER>38087</CUSTOMER_NUMBER>
<CUSTOMER_NAME>UNIVERSE SELLER</CUSTOMER_NAME>
<LINE>
<LINE_ROW>
<HEADER_ID>5001507</HEADER_ID>
<LINE_ID>12532839</LINE_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<ITEM_NUMBER>STAGEPAS 600I-CA</ITEM_NUMBER>
<ORDER_QUANTITY>5</ORDER_QUANTITY>
</LINE_ROW>
<LINE_ROW>
<HEADER_ID>5001507</HEADER_ID>
<LINE_ID>12532901</LINE_ID>
<LINE_NUMBER>3</LINE_NUMBER>
<ITEM_NUMBER>CD-C600 RK</ITEM_NUMBER>
<ORDER_QUANTITY>6</ORDER_QUANTITY>
</LINE_ROW>
<LINE_ROW>
<HEADER_ID>5001507</HEADER_ID>
<LINE_ID>12532902</LINE_ID>
<LINE_NUMBER>4</LINE_NUMBER>
<ITEM_NUMBER>CD-S300 RK</ITEM_NUMBER>
<ORDER_QUANTITY>8</ORDER_QUANTITY>
</LINE_ROW>
</LINE>
<PRCADJ>
<PRCADJ_ROW>
<PRICE_ADJUSTMENT_ID>43095064</PRICE_ADJUSTMENT_ID>
<HEADER_ID>5001507</HEADER_ID>
<LINE_ID>12532839</LINE_ID>
<ADJUSTED_AMOUNT>-126</ADJUSTED_AMOUNT>
</PRCADJ_ROW>
<PRCADJ_ROW>
<PRICE_ADJUSTMENT_ID>43095068</PRICE_ADJUSTMENT_ID>
<HEADER_ID>5001507</HEADER_ID>
<LINE_ID>12532840</LINE_ID>
<ADJUSTED_AMOUNT>-96.6</ADJUSTED_AMOUNT>
</PRCADJ_ROW>
</PRCADJ>
</ROW>
</ROWSET>
The issue is the Parent can have multiple child and each child can multiple sub child. How can i write query to transfer this into Sql Server 2005
You need to use three CROSS APPLY operators to break up the "list of XML elements" into separate pseudo tables of XML rows, so you can access their properties - something like this:
SELECT
HeaderID = XCRow.value('(HEADER_ID)[1]', 'int'),
OrderNumber = XCRow.value('(ORDER_NUMBER)[1]', 'int'),
LineHeaderID = XCLine.value('(HEADER_ID)[1]', 'int'),
LineID = XCLine.value('(LINE_ID)[1]', 'int'),
LineNumber = XCLine.value('(LINE_NUMBER)[1]', 'int'),
PriceAdjustmentID = XCPrc.value('(PRICE_ADJUSTMENT_ID)[1]', 'int'),
AdjustedAmount = XCPrc.value('(ADJUSTED_AMOUNT)[1]', 'decimal(20,4)')
FROM
dbo.YourTableNameHere
CROSS APPLY
Data.nodes('/ROWSET/ROW') AS XTRow(XCRow)
CROSS APPLY
XCRow.nodes('LINE/LINE_ROW') AS XTLine(XCLine)
CROSS APPLY
XCRow.nodes('PRCADJ/PRCADJ_ROW') AS XTPrc(XCPrc)
With this, the first CROSS APPLY will handle all the elements that are found directly under <ROWSET> / <ROW> (the header information), the second one will enumerate all instances of <LINE> / <LINE_ROW> below that header element, and the third CROSS APPLY handles the <PRCADJ> / <PRCADJ_ROW> elements, also below the header.
You might need to tweak the outputs a bit - and I only picked two or three of the possible values - extend and adapt to your own needs! But this should show you the basic mechanism - the .nodes() method returns a "pseudo table" of XML fragments, one for each match of the XPath expression you define.
you can do some thing like this. using cross apply you will get node elements and then extract the value using value clause. you need to specify the column type i.e int or varchar etc.
The result can then be inserted using insert into select query.
insert into Table1 values ( header_id, order_number, cust_po_number)
select R.value('(HEADER_ID)[1]', 'int') As header_id,
R.value('(ORDER_NUMBER)[1]', 'int') as order_number,
R.value('(CUST_PO_NUMBER)[1]', 'varchar(256)') as cust_po_number
from table
cross apply XMLdata.nodes('/ROWSET/ROW') AS P(R)
insert into Table2 values ( header_id, line_id, line_number)
select R.value('(HEADER_ID)[1]', 'int') As header_id,
R.value('(LINE_ID)[1]', 'int') as line_id,
R.value('(LINE_NUMBER)[1]', 'int') as line_number
from table
cross apply XMLdata.nodes('/ROWSET/ROW/LINE/LINE_ROW') AS P(R)
I have a sample xml file that I need to upload into a SQL table.
Below the xml file is the SQL query to select the XML values to insert into the table
Problem: I am unable to obtain the <Actual>,<ExpectedValue> values as they occur more than once
Would appreciate if someone could kindly help correct/direct me to get the right values
Thanks.
<?xml version="1.0" encoding="utf-8" ?>
<Results>
<General>
<Version>1.01</Version>
<InputFilename>TestFile.xml</InputFilename>
<Filename>xyz.XML</Filename>
</General>
<Tests>
<Test Name="Test 1">
<Number>0</Number>
<MinimumSampleCount>1</MinimumSampleCount>
<ExpectedValues>
<ExpectedValue>8</ExpectedValue>
<ExpectedValue>5</ExpectedValue>
</ExpectedValues>
<Actual>
<seq>1</seq>
<ValueFound>8</ValueFound>
<seqTestResult>Passed</seqTestResult>
</Actual>
<Actual>
<seq>3</seq>
<ValueFound>8</ValueFound>
<seqTestResult>Passed</seqTestResult>
</Actual>
<Result>Last sequence matches test cases.</Result>
<TestResult>Passed</TestResult>
</Test>
<Test Name="Test 3">
<Number>25</Number>
<ExpectedValues><ExpectedValue>3.50</ExpectedValue>
<ExpectedValue>3.56</ExpectedValue>
<ExpectedValue>3.60</ExpectedValue>
</ExpectedValues>
<Result>not applicable</Result>
<TestResult>Skipped</TestResult>
</Test>
<OverallTestResult>Passed</OverallTestResult>
</Tests>
</Results>
SQL Code:
INSERT INTO dbo.Results_XML ([Version],[InputFilename],[Filename],
[OverallTestResult],Name,Number,MinimumSampleCount,ActualNumberOfSamples,ExpectedValue,
ActualSeq, ActualValueFound,ActualseqTestResult,CompareValue,Result,TestResult
SELECT [Version] = x.data.value('Version[1]','numeric(3,2)'),
[InputFilename] = x.data.value('InputFilename[1]','varchar(80)'),
[Filename] = x.data.value('Filename[1]','varchar(80)'),
[OverallTestResult]=T2.N.value('OverallTestResult[1]','varchar(15)'),
[Test Name]= y.data.value('#Name','varchar(255)')
,[Number]= y.data.value('Number[1]','int'),
[MinimumSampleCount]=y.data.value('Number[1]','int'),
[ActualNumberOfSamples]=y.data.value('ActualNumberOfSamples[1]','int')
,[ExpectedValue]=z.data.value('ExpectedValue[1]','varchar(255)')
,[ActualSeq]=v.data.value('Seq[1]','int'),
,ActualValueFound=v.data.value('ValueFound[1]','int'),
,ActualseqTestResult=v.data.value('seqTestResult[1]','varchar(255)'),
,[CompareValue]=y.data.value('CompareValue[1]','bigint')
,[Result]=y.data.value('Result[1]','varchar(40)'),
[TestResult]=y.data.value('TestResult[1]','varchar(10)')
FROM CD t
CROSS APPLY t.XMLData.nodes('/Results/General') x(data)
cross apply t.XMLData.nodes('/Results/Tests') as T2(N)
CROSS APPLY t.XMLData.nodes('/Results/Tests/Test') as y(data)
CROSS APPLY t.XMLData.nodes('Test/ExpectedValues/Expected') z(data)
CROSS APPLY t.XMLData.nodes('Test/ExpectedValues/Actual/') v(data)
I'm assuming (based on your previous question) that this is for SQL Server - correct?
(you should always explicitly and clearly specify what database system you're using - get used to it!)
You need to use something like this:
get a XML node for the <Results>/<General> node to get some basic data
then get a list of all <Test> nodes - get some data from there
based on each <Test> node - get a list of it's <ExpectedValues>/<ExpectedValue> nodes as well as the <Actual> nodes inside the <Test> and pick out the data that you need
In my experience, it's a good idea to use descriptive names - use GeneralData, ExpectedData etc. instead of just x, y and z - that makes it a lot easier to understand what you're dealing with!
Here's the T-SQL / XQuery statement that I used:
SELECT
-- General values
[Version] = GeneralData.value('Version[1]','numeric(3,2)'),
[InputFilename] = GeneralData.value('InputFilename[1]','varchar(80)'),
[Filename] = GeneralData.value('Filename[1]','varchar(80)'),
-- Test-specific values
[Test Name]= TestData.value('#Name','varchar(255)'),
[Test Number]= TestData.value('Number[1]','int'),
-- Test-specific expected values
[ExpectedValue] = ExpectedData.value('.', 'varchar(255)'),
-- Test-specific actual values
[ActualSeq] = ActualData.value('(seq)[1]', 'int'),
ActualValueFound = ActualData.value('(ValueFound)[1]', 'int'),
ActualseqTestResult = ActualData.value('(seqTestResult)[1]', 'varchar(255)')
FROM
CD t
CROSS APPLY
-- fetch the <Results> / <General> node
XMLdata.nodes('/Results/General') AS XGD(GeneralData)
CROSS APPLY
-- fetch a list of <Results> / <Tests> / <Test> nodes
XMLDATA.nodes('/Results/Tests/Test') AS XTD(TestData)
CROSS APPLY
-- for each <Test> node - fetch its contained <ExpectedValues>/<ExpectedValue> nodes
TestData.nodes('ExpectedValues/ExpectedValue') AS XED(ExpectedData)
CROSS APPLY
-- for each <Test> node - fetch its contained <Actual> nodes
TestData.nodes('Actual') AS XAD(ActualData)