SQL Server - XQuery: delete parent node based on child value substring - sql-server

I want to delete all parent nodes TxDtls of the following XML where position 20 of child value Ref is 2.
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04 camt.054.001.04.xsd">
<BkToCstmrDbtCdtNtfctn>
<Ntfctn>
<Ntry>
<TtlChrgsAndTaxAmt Ccy="CHF">1.60</TtlChrgsAndTaxAmt>
<Rcrd>
<Amt Ccy="CHF">1.60</Amt>
<CdtDbtInd>DBIT</CdtDbtInd>
<ChrgInclInd>false</ChrgInclInd>
<Tp>
<Prtry>
<Id>2</Id>
</Prtry>
</Tp>
</Rcrd>
</Chrgs>
<NtryDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111118144400000000020076766</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111117645600000000030076281</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
</Ntfctn>
</BkToCstmrDbtCdtNtfctn>
</Document>
So I want to delete the first TxDtls node (substring position 20 = 2) while I want to keep the second one (substring position 20 <> 2).
I tried this:
UPDATE mytable SET XMLData.modify('delete .//TxDtls[RmtInf/Strd/CdtrRefInf/Ref/substring(text(),20,1) = ''2'']')
However, I get the error "The XQuery syntax '/function()' is not supported". Any hints on how to achieve this?
Thanks

What a difference made by the partially provided minimal reproducible example.
The XML is still not well-formed. I had to comment out the following tag: </Chrgs>
A default namespace is easily handled by its declaration in the XQuery method.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO #tbl (xmldata) VALUES
(N'<?xml version="1.0"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04 camt.054.001.04.xsd">
<BkToCstmrDbtCdtNtfctn>
<Ntfctn>
<Ntry>
<TtlChrgsAndTaxAmt Ccy="CHF">1.60</TtlChrgsAndTaxAmt>
<Rcrd>
<Amt Ccy="CHF">1.60</Amt>
<CdtDbtInd>DBIT</CdtDbtInd>
<ChrgInclInd>false</ChrgInclInd>
<Tp>
<Prtry>
<Id>2</Id>
</Prtry>
</Tp>
</Rcrd>
<!--</Chrgs>-->
<NtryDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111118144400000000020076766</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111117645600000000030076281</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
</Ntfctn>
</BkToCstmrDbtCdtNtfctn>
</Document>');
-- DDL and sample data population, end
-- before
SELECT * FROM #tbl;
UPDATE #tbl SET xmldata.modify('declare default element namespace "urn:iso:std:iso:20022:tech:xsd:camt.054.001.04";
delete /Document/BkToCstmrDbtCdtNtfctn/Ntfctn/Ntry/NtryDtls/TxDtls[RmtInf/Strd/CdtrRefInf/Ref[substring(./text()[1],20,1) = "2"]]');
-- after
SELECT * FROM #tbl;

Related

Retrieve associated value from next node for each tag

I have the following XML:
<Envelope format="ProceedoOrderTransaction2.1">
<Sender>SENDER</Sender>
<Receiver>RECEIVER</Receiver>
<EnvelopeID>xxxxx</EnvelopeID>
<Date>2021-05-06</Date>
<Time>11:59:46</Time>
<NumberOfOrder>1</NumberOfOrder>
<Order>
<Header>
<OrderNumber>POXXXXX</OrderNumber>
</Header>
<Lines>
<Line>
<LineNumber>1</LineNumber>
<ItemName>Ipsum Lorum</ItemName>
<SupplierArticleNumber>999999</SupplierArticleNumber>
<UnitPrice vatRate="25.0">50</UnitPrice>
<UnitPriceBasis>1</UnitPriceBasis>
<OrderedQuantity unit="Styck">200</OrderedQuantity>
<AdditionalItemProperty Key="ARTIKELNUMMER" Description="Unik ordermärkning (artikelnummer):" />
<Value>999999</Value>
<AdditionalItemProperty Key="BESKRIVNING" Description="Kort beskrivning:" />
<Value>Ipsum Lorum</Value>
<AdditionalItemProperty Key="BSKRIVNING" Description="Beskrivning:" />
<Value>Ipsum Lorum</Value>
<AdditionalItemProperty Key="ENHET" Description="Enhet:" />
<Value>Styck</Value>
<AdditionalItemProperty Key="KVANTITET" Description="Kvantitet:" />
<Value>200</Value>
<AdditionalItemProperty Key="PRIS" Description="Pris/Enhet (ex. moms):" />
<Value>50</Value>
<AdditionalItemProperty Key="VALUTA" Description="Valuta:" />
<Value>SEK</Value>
<Accounting>
<AccountingLine amount="10000">
<AccountingValue dimensionPosition="001" dimensionExternalID="ACCOUNT">xxx</AccountingValue>
<AccountingValue dimensionPosition="002" dimensionExternalID="F1">Ipsum Lorum</AccountingValue>
<AccountingValue dimensionPosition="005" dimensionExternalID="F3">1</AccountingValue>
<AccountingValue dimensionPosition="010" dimensionExternalID="F2">9999</AccountingValue>
</AccountingLine>
</Accounting>
</Line>
</Lines>
</Order>
</Envelope>
I am able to parse out all values correctly to table structure except for 1 value in a way that ensures its it associated with its tag. So where I stumble is that I am correctly getting 1 row per AdditionalItemProperty and I am able to get the Key and Description tag values, for example BESKRIVNING and Kort beskrivning:, but I can't (in a reasonable way) get the value between <Value> </Value> brackets that is also associated with each tag value. So for tag key value BESKRIVNING the associated value is 99999 which seem to be on same hierarchy level (insane I know) as the AdditionalItemProperty it is associated with. Seems like they use logic that value for a AdditionalItemProperty will be following the AdditionalItemProperty tag.
I am using SQL Server 2019. I have gotten this far:
-- Purchaseorderrowattributes
select top(10)
i.value(N'(./Header/OrderNumber/text())[1]', 'nvarchar(30)') as OrderNumber,
ap.value(N'(../LineNumber/text())[1]', 'nvarchar(30)') as LineNumber,
ap.value(N'(#Description)', 'nvarchar(50)') property_description
from
load.proceedo_orders t
outer apply
t.xml_data.nodes('Envelope/Order') AS i(i)
outer apply
i.nodes('Lines/Line/AdditionalItemProperty') as ap(ap)
where
file_name = #filename
Which produces the following output:
OrderNumber LineNumber property_description
--------------------------------------------
PO170006416 1 Antal timmar
PO170006416 1 Beskrivning
PO170006416 1 Kompetensområde
PO170006416 1 Roll
PO170006416 1 Ordernummer
PO170006416 1 Timpris
I can't find a way to add the value for each property in a correct way. Since the ordering of the values will always be same as the ordering of the AdditionalItemProperty i found solution to get ordering of the AdditionalItemProperty and then i could use rownumber and i was then hoping to input the rownumber value into the bracket in
ap.value(N'(../Value/text())[1]', 'nvarchar(50)') property_description
but SQL Server throws exception that it has to be string literal.
So to be clear what I tried doing with something like:
ap.value(CONCAT( N'(../Value/text())[', CAST(ROWNUMBER as varchar) ,']'), 'nvarchar(50)') property_description
SQL Server uses XQuery 1.0.
You can make use of the >> Node Comparison operator to find the Value sibling node with code similar to the following:
-- Purchaseorderrowattributes
select top(10)
i.value(N'(./Header/OrderNumber/text())[1]', 'nvarchar(30)') as OrderNumber
,ap.value(N'(../LineNumber/text())[1]', 'nvarchar(30)') as LineNumber
,ap.value(N'(#Description)', 'nvarchar(50)') property_description
,ap.query('
let $property := .
return (../Value[. >> $property][1])/text()
').value('.[1]', 'varchar(20)') as property_value
from load.proceedo_orders t
outer apply t.xml_data.nodes('Envelope/Order') AS i(i)
outer apply i.nodes('Lines/Line/AdditionalItemProperty') as ap(ap)
where file_name = #filename
So what's going on here?
let $property := . is creating a reference to the current AdditionalItemProperty node.
../Value[. >> $property] is ascending to the parent node, Line, and descending again to find Value nodes after the AditionalItemProperty node reference in document order, with [1] selecting the first one of those nodes.
See the 3.5.3 Node Comparisons section for a little more detail.

extract soap xml tag value containing multiple namespaces and different attribute in SQL Sever

Below is the sample SOAP xml and query to extract the value -
DECLARE #xml XML='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<xsi:MaintenanceOrder xmlns:xsi="http://schema.xyz.com/abc/2" xmlns:ush="http://www.xyz.nl/abc" xmlns:xsj="http://www.w3.org/2001/XMLSchema-instance" languageCode="en-US" releaseID="9.2" systemEnvironmentCode="Production" versionID="2.8.0">
<xsi:DataArea>
<xsi:MaintenanceOrder>
<xsi:MaintenanceOrderHeader>
<xsi:UserArea>
<xsi:Property>
<xsi:NameValue accountingEntity="*" listID="*" name="OrderDate" type="DATE">2020-03-30T00:00:00</xsi:NameValue>
</xsi:Property>
<xsi:Property>
<xsi:NameValue accountingEntity="*" listID="*" name="ReportDate" type="DATE">2020-04-30T00:00:00</xsi:NameValue>
</xsi:Property>
</xsi:UserArea>
</xsi:MaintenanceOrderHeader>
</xsi:MaintenanceOrder>
</xsi:DataArea>
</xsi:MaintenanceOrder>
</soapenv:Body>
</soapenv:Envelope>'
select A.r.value('(xsi:NameValue[#name="OrderDate"])[1]','date') as "OrderDate"
,A.r.value('(xsi:NameValue[#name="ReportDate"])[1]','date') as "ReportDate"
FROM #xml.nodes('/*:Envelope/*:Body/*:MaintenanceOrder/*:DataArea/*:MaintenanceOrder/*:MaintenanceOrderHeader/*:UserArea/*:Property') AS A(r)
issue 1 - by removing namespace in MaintenanceOrder only then query returns value otherwise returns null.
issue 2 - the required output is single line with multiple tags value, but the query is giving multiple rows.
Any help would be highly appreciated.
Try to use with xmlnamespaces declarations and explicit paths so that your XPath queries match only the elements you expect - and perform as well as can be expected.
For example:
declare #xml xml =
N'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<xsi:MaintenanceOrder xmlns:xsi="http://schema.xyz.com/abc/2" xmlns:ush="http://www.xyz.nl/abc" xmlns:xsj="http://www.w3.org/2001/XMLSchema-instance" languageCode="en-US" releaseID="9.2" systemEnvironmentCode="Production" versionID="2.8.0">
<xsi:DataArea>
<xsi:MaintenanceOrder>
<xsi:MaintenanceOrderHeader>
<xsi:UserArea>
<xsi:Property>
<xsi:NameValue accountingEntity="*" listID="*" name="OrderDate" type="DATE">2020-03-30T00:00:00</xsi:NameValue>
</xsi:Property>
<xsi:Property>
<xsi:NameValue accountingEntity="*" listID="*" name="ReportDate" type="DATE">2020-04-30T00:00:00</xsi:NameValue>
</xsi:Property>
</xsi:UserArea>
</xsi:MaintenanceOrderHeader>
</xsi:MaintenanceOrder>
</xsi:DataArea>
</xsi:MaintenanceOrder>
</soapenv:Body>
</soapenv:Envelope>';
with xmlnamespaces (
'http://schemas.xmlsoap.org/soap/envelope/' as s11,
'http://schema.xyz.com/abc/2' as abc
)
select
A.r.value('(abc:Property/abc:NameValue[#name="OrderDate"])[1]','date') as "OrderDate",
A.r.value('(abc:Property/abc:NameValue[#name="ReportDate"])[1]','date') as "ReportDate"
from #xml.nodes('/s11:Envelope/s11:Body/abc:MaintenanceOrder/abc:DataArea/abc:MaintenanceOrder/abc:MaintenanceOrderHeader/abc:UserArea') as A(r);
Returns the expected values in a single row:
OrderDate ReportDate
2020-03-30 2020-04-30

How to add additional attributes and XML namespaces value inside same root in SQL Server

I am trying to add attributes and xmlns using WITH XMLNAMESPACES under same root, but could not make it work. Please help.
The following SQL query throws an error
Msg 6852, Level 16, State 1, Line 17
Attribute-centric column '#encounter_number' must not come after a non-attribute-centric sibling in XML hierarchy in FOR XML PATH.
for this line
'HMSXML' AS "pbrc/#source" , URNumber AS "pbrc/#messageHash", 'xmlResult01' AS "pbrc/#integration_info"
Full code:
DECLARE #xml XML;
WITH XMLNAMESPACES (
'http://www.w3.org/2001/XMLSchema-instance' as xsi
,'http://java.sun.com/xml/ns/jaxb/xjc' as xjc
,'http://www.powerhealthsolutions.com/pbrc/jaxb/ext' as [pbrc-jaxb]
,'http://java.sun.com/xml/ns/jaxb' AS jaxb
,DEFAULT 'http://www.powerhealthsolutions.com/pbrc')
SELECT
#xml = (SELECT
'HMSXML' AS "pbrc/#source",
URNumber AS "pbrc/#messageHash",
'xmlResult01' AS "pbrc/#integration_info",
AdmissionID AS "#encounter_number",
'NDIS' AS organisation_code,
'NDIS' AS encounter_type,
(SELECT
'true' AS "#delete",
(SELECT 'NDIS' AS class_type, AdmissionDate AS start_date_time
FOR XML PATH ('financial_class'), TYPE)
FOR XML PATH('financial_classes'), TYPE),
(SELECT
URNumber AS "#entity_number",
(SELECT 'NDIS ID' AS name, NDISNumber AS value
FOR XML PATH ('extra'), ROOT('extras'), TYPE)
FOR XML PATH ('recipient'), TYPE),
'H' AS referral_source,
'H' AS separation_status,
AdmissionDate AS start_date_time
FROM
Episode
FOR XML PATH('encounter'), ROOT('pbrc'), TYPE)
select #xml
I want to get output as shown here, but I'm failing to achieve those last 3 attributes integration_info="xmlResult03", messageHash="4208151" and source="HMSXML". These will come from SQL select query.
<?xml version="1.0"?>
-<pbrc xmlns="http://www.powerhealthsolutions.com/pbrc" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:pbrc-jaxb="http://www.powerhealthsolutions.com/pbrc/jaxb/ext" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" integration_info="xmlResult03" messageHash="4208151" source="HMSXML">
-<encounter encounter_number="525241">
<organisation_code>NDIS</organisation_code>
<encounter_type>NDIS</encounter_type>
-<financial_classes delete="true">
-<financial_class>
<class_type>NDIS</class_type>
<start_date_time>2018-06-26T00:00:00</start_date_time>
</financial_class>
</financial_classes>
-<payors>
-<payor>
<payor entity_number="4208151"> </payor>
<slot>Patient</slot>
<start_date>2018-06-26</start_date>
</payor>
</payors>
-<recipient entity_number="4208151">
-<extras>
-<extra>
<name>NDIS ID</name>
<value>430392519</value>
</extra>
</extras>
</recipient>
<referral_source>H</referral_source>
<separation_status>H</separation_status>
-<services>
-<service>
<source_system_code>HMSXML</source_system_code>
<organisation_code>NDIS</organisation_code>
<actual_charge>179.26</actual_charge>
<description1>Arnold Sch - Occupational Therapist</description1>
<quantity>1</quantity>
<service_code>15_048_0128_1_3</service_code>
<start_time>2019-03-27T15:00:00</start_time>
</service>
-<service>
<source_system_code>HMSXML</source_system_code>
<organisation_code>NDIS</organisation_code>
<actual_charge>193.99</actual_charge>
<description1>Arnold Sch - Occupational Therapist</description1>
<quantity>1</quantity>
<service_code>15_056_0128_1_3</service_code>
<start_time>2019-07-30T15:00:00</start_time>
</service>
</services>
<start_date_time>2018-06-26T00:00:00</start_date_time>
</encounter>
</pbrc>

SQL replace value of with always random xml code

I have the following SQL XML in several rows of a table (table is tbldatafeed column in configuration_xml). All of the UserName="" and Password="" is different each time for each row and does not repeat so I can not find/replace off of that. I am trying to write a query that finds all of those and replaces them with Username/Passwords I choose.
<DataFeed xmlns="http://www.tech.com/datafeed/dfx/2010/04" xmlns:plugin="pluginExtensions" Type="TODO" Guid="TODO" UserAccount="DF_LEAN_PopulateCommentsSubForm" Locale="en-US" DateFormat="" ThousandSeparator="" NegativeSymbol="" DecimalSymbol="" SendingNotifications="false" SendJobStatusNotifications="false" RecipientUserIds="" RecipientGroupIds="" RecipientEmailAddresses="" Name="CI_C11.01_Lean-Lean_Reject Comments_A2A" >
<Transporter>
<transporters:ArcherWebServiceTransportActivity xmlns:transporters="clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed" xmlns:out="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:compModel="clr-namespace:ArcherTech.DataFeed.ComponentModel;assembly=ArcherTech.DataFeed" xmlns:channel="clr-namespace:ArcherTech.DataFeed.Engine.Channel;assembly=ArcherTech.DataFeed" xmlns:engine="clr-namespace:ArcherTech.DataFeed.Engine;assembly=ArcherTech.DataFeed" xmlns:kernel="clr-namespace:ArcherTech.Kernel.Channel;assembly=ArcherTech.Kernel" xmlns="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:schema="clr-namespace:System.Xml.Schema;assembly=System.Xml" xmlns:xmlLinq="clr-namespace:System.Xml.Linq;assembly=System.Xml" xmlns:domain="clr-namespace:ArcherTech.Common.Domain;assembly=ArcherTech.Common" xmlns:s="clr-namespace:System;assembly=mscorlib" x:Key="transportActivity" SearchType="ReportId" Uri="https://arcs-d" RecordsPerFile="100" ReportID="EC514865-88D5-49CE-A200-7769EC1C2A88" UseWindowsAuth="false" IsWindowsAuthSpecific="false" WindowsAuthUserName="i9XzCczAQ7J2rHwkg6wG9QF8+O9NCYJZP6y5Kzw4be0+cdvUaGu/9+rHuLstU736pnQrRcwmnSIhd6oPKIvnLA==" WindowsAuthPassword="+y0tCAKysxEMSGv1unpHxfg6WjH5XWylgP45P5MLRdQ6+zAdOLSVy7s3KJa3+9j2i83qn8I8K7+1+QBlCJT1E7sLQHWRFOCEdJgXaIr1gWfUEO+7kjuJnZcIEKZJa2wHyqc2Z08J2SKfdCLh7HoLtg==" WindowsAuthDomain="" ProxyName="" ProxyPort="8080" ProxyUsername="" ProxyPassword="" ProxyDomain="" IsProxyActive="False" ProxyOption="None" InstanceName="ARCS-D" TempFileOnSuccessAction="DoNothing" TempFileOnSuccessRenameString="" TempFileOnErrorAction="DoNothing" TempFileOnErrorRenameString="" Transform="{engine:DataFeedBinding Path=Transform}" SessionContext="{engine:DataFeedBinding Path=Session}">
<transporters:ArcherWebServiceTransportActivity.Credentials>
<NetworkCredentialWrapper UserName="TeSZmI1SqO0eJ0G2nDVU+glFg/9eZfeMppYQnPfbeg8=" Password="Slt4VHqjkYscWyCwZK40QJ7KOQroG9OTKr+RGt9bQjE=" />
</transporters:ArcherWebServiceTransportActivity.Credentials>
</transporters:ArcherWebServiceTransportActivity>
</Transporter>
</DataFeed>
I need to be able to set a value and replace it with a query
I have written the following
select #config_xml=configuration_xml from bldatafeed where datafeed_name = 'REMOVED'
update tbldatafeed set configuration_xml.modify(//*:NetworkCredentialWrapper/#UserName)[1] with "abc" ')
where datafeed_name = 'REMOVED'
This does the trick but it only works if I set the "abc" password each time in each area and in some cases I am running this against 50+ rows.
I also tried:
Declare #server nvarchar(max) = 'abc'
Declare #config_xml xml
select #config_xml=configuration_xml from bldatafeed where datafeed_name = 'REMOVED'
update tbldatafeed set configuration_xml.modify(//*:NetworkCredentialWrapper/#UserName)[1] with #server ')
where datafeed_name = 'REMOVED'
The error from this is that: XQuery [tbldatafeed.configuration_xml.modify()]: Top-level attribute nodes are not supported
What I would like to be able to do is set my variable and utilize that as I will be setting this up for multiple rows and unfortunately this error is making this a very difficult problem to solve.
Thanks for any help, this has kept me confused for a bit.
Use the function sql:variable() to use a variable in the XQuery expression.
declare #T table(X xml);
insert into #T values('<X UserName=""/>');
declare #UserName nvarchar(max) = 'abc'
update #T set
X.modify('replace value of (/X/#UserName)[1]
with sql:variable("#UserName")');

MSSQL Parsing of XML that Contains same Sibling

I am trying to query a MSSQL database that has XML and parse it out.
here is the data:
<Root>
<Relatives>
<Relative>
<Relation>Father</Relation>
<BcAge>50</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
<Relative>
<Relation>Mother</Relation>
<BcAge>58</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
Here is the desired result
Relation BcAge BilatAge OcAge Relation BcAge BilatAge OcAge
Father 50 0 0 Mother 58 0 0
I doubt, that you really want to get your result side-by-side... This is no valid resultset, your column names are duplicated.
If you really need it this way you might do this:
DECLARE #xml XML=
N'<Root>
<Relatives>
<Relative>
<Relation>Father</Relation>
<BcAge>50</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
<Relative>
<Relation>Mother</Relation>
<BcAge>58</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
</Relatives>
</Root>';
The query will use a CTE to get the Father and the Mother with an XQuery-predicate
WITH Parents AS
(
SELECT #xml.query(N'/Root/Relatives/Relative[(Relation/text())[1]="Father"]') AS Father
,#xml.query(N'/Root/Relatives/Relative[(Relation/text())[1]="Mother"]') AS Mother
)
SELECT 'Father' AS F_Relation
,Father.value(N'(/Relative/BcAge)[1]',N'int') AS F_BcAge
,Father.value(N'(/Relative/BilatAge)[1]',N'int') AS F_BilatAge
,Father.value(N'(/Relative/OcAge)[1]',N'int') AS F_OcAge
,'Mother' AS M_Relation
,Mother.value(N'(/Relative/BcAge)[1]',N'int') AS M_BcAge
,Mother.value(N'(/Relative/BilatAge)[1]',N'int') AS M_BilatAge
,Mother.value(N'(/Relative/OcAge)[1]',N'int') AS M_OcAge
FROM Parents;
But probably it's this you are looking for (any count of <Relative>):
SELECT rel.value(N'(Relation/text())[1]',N'nvarchar(max)') AS Relation
,rel.value(N'(BcAge/text())[1]',N'int') AS BcAge
,rel.value(N'(BilatAge/text())[1]',N'int') AS BilatAge
,rel.value(N'(OcAge/text())[1]',N'int') AS OcAge
FROM #xml.nodes(N'/Root/Relatives/Relative') AS A(rel)

Resources