How to query XML in T-SQL with inline ID - sql-server

I have this XML stored in a record, how can I get a value of a specific ID?
<Attributes>
<CustomerAttribute ID="4">
<CustomerAttributeValue>
<Value>1</Value>
</CustomerAttributeValue>
</CustomerAttribute>
<CustomerAttribute **ID="5"**>
<CustomerAttributeValue>
<Value>**aaaaa**</Value>
</CustomerAttributeValue>
</CustomerAttribute>

Assuming the ** surrounding ID="5" and "aaaaa" denotes the nodes you want to select, below is one method.
SELECT XmlColumn.value('(/Attributes/CustomerAttribute[#ID="5"]/CustomerAttributeValue/Value)[1]', 'varchar(100)')
FROM dbo.xml
WHERE XmlColumn.exist('/Attributes/CustomerAttribute[#ID="5"]') = 1;
This assumes the actual XML value is like:
<Attributes>
<CustomerAttribute ID="4">
<CustomerAttributeValue>
<Value>1</Value>
</CustomerAttributeValue>
</CustomerAttribute>
<CustomerAttribute ID="5">
<CustomerAttributeValue>
<Value>aaaaa</Value>
</CustomerAttributeValue>
</CustomerAttribute>
</Attributes>

Related

TSQL Parse from XML nodes

This XML comes from public treasury. SQL Server seems to think it is not XML? My result says the XLT method can only be involved with column type XML. Is this result set from the treasury really XML?
How can I parse it into a SQL table?
treasury daily market yield
DECLARE #XML NVARCHAR(MAX)
SELECT #XML =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">DailyTreasuryYieldCurveRateData</title>
<id>http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData</id>
<updated>2020-07-14T05:40:26Z</updated>
<link rel="self" title="DailyTreasuryYieldCurveRateData" href="DailyTreasuryYieldCurveRateData"/>
<entry>
<id>http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7633)</id>
<title type="text"/>
<updated>2020-07-14T05:40:26Z</updated>
<author>
<name/>
</author>
<link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7633)"/>
<category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">7633</d:Id>
<d:NEW_DATE m:type="Edm.DateTime">2020-07-01T00:00:00</d:NEW_DATE>
<d:BC_1MONTH m:type="Edm.Double">0.12</d:BC_1MONTH>
<d:BC_2MONTH m:type="Edm.Double">0.12</d:BC_2MONTH>
<d:BC_3MONTH m:type="Edm.Double">0.14</d:BC_3MONTH>
<d:BC_6MONTH m:type="Edm.Double">0.17</d:BC_6MONTH>
<d:BC_1YEAR m:type="Edm.Double">0.16</d:BC_1YEAR>
<d:BC_2YEAR m:type="Edm.Double">0.17</d:BC_2YEAR>
<d:BC_3YEAR m:type="Edm.Double">0.19</d:BC_3YEAR>
<d:BC_5YEAR m:type="Edm.Double">0.31</d:BC_5YEAR>
<d:BC_7YEAR m:type="Edm.Double">0.52</d:BC_7YEAR>
<d:BC_10YEAR m:type="Edm.Double">0.69</d:BC_10YEAR>
<d:BC_20YEAR m:type="Edm.Double">1.2</d:BC_20YEAR>
<d:BC_30YEAR m:type="Edm.Double">1.43</d:BC_30YEAR>
<d:BC_30YEARDISPLAY m:type="Edm.Double">1.43</d:BC_30YEARDISPLAY>
</m:properties>
</content>
</entry>
</feed>'
SELECT b.value('#d:Id', 'varchar(28)') as d_id
,b.value('#d:BC_30YEAR', 'double') as d_BC_30YEAR
FROM #XML.nodes('feed/entry/content type/m:properties/') as a(b)
First, define your #XML to be of type XML:
DECLARE #XML XML;
Then: you need to define the relevant XML namespaces involved!
Try this snippet of code:
-- define XML namespaces!
;WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2005/Atom',
'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' AS m,
'http://schemas.microsoft.com/ado/2007/08/dataservices' AS d)
SELECT
b.value('(d:Id)[1]', 'varchar(28)') as d_id,
b.value('(d:BC_30YEAR)[1]', 'decimal(20,2)') as d_BC_30YEAR
FROM
#XML.nodes('feed/entry/content/m:properties') as a(b)
This returns the following value:
Fixes I made:
FROM #XML.nodes('feed/entry/content type/m:properties/') as a(b)
^^^^^^^^^^^^
Wrong node name - the node is <content> (not "content type") - the type is just an attribute on the node, no relevant in this XPath here.
SELECT b.value('#d:Id', 'varchar(28)') as d_id
^^^^^^
You really want to select the XML element (node) d:Id - not an attribute - the # in #d:Id denotes an attribute!
b.value('#d:BC_30YEAR', 'double') as d_BC_30YEAR
^^^^^^^^^
You need to use a proper T-SQL datatype - like decimal(20,2) - not "double"
here....

Query to remove a tag in XML in SQL Server

I have a xml with name space as follows. How do I remove the tag <ns1:replyTo> from the XML using XML query. Can we remove the tag with something like below query. Am not sure how to pass through the tags with SOAP-ENV
update tablename
set XMLColumnname.modify(('delete /ServiceIn/file/sender/replyTo[1]'))
XML:
<?xml version="1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:si="http://someservice.abc.com/stds/xyz/a1"
xmlns:oas="http://docs.oasis-open.org/abc/2004/01/oasis-200401-abc-abcsecurity-xyz- 1.0.xsd">
<SOAP-ENV:Header>
<oas:Security>
<oas:Credential>
<oas:name>%s</oas:name>
<oas:Password>%s</oas:Password>
</oas:Credential>
</oas:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:execute
xmlns:ns1="urn:com.company.us.abc.service.xyz.someService">
<si:ServiceRequestInfo>
<si:Tag1 />
<si:Tag2 />
<si:Tag3 />
<si:Tag4 />
</si:ServiceRequestInfo>
<ns1:ServiceIn>
<ns1:Tag5>%s</ns1:Tag5>
<ns1:File>
<ns1:Sender>
<ns1:Name>%s</ns1:Name>
<ns1:Email>%s</ns1:Email>
<ns1:replyTo>%s</ns1:replyTo>
</ns1:Sender>
</ns1:File>
</ns1:ServiceIn>
</ns1:execute>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Your current XQuery path is anonymous and also starts at the wrong root.
You need to define the required namespaces using with xmlnamespaces or declare the namespaces inside the XQuery statement itself, e.g.:
create table tablename (
XMLColumnname xml
);
insert tablename values
(N'<?xml version="1.0"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:si="http://someservice.abc.com/stds/xyz/a1"
xmlns:oas="http://docs.oasis-open.org/abc/2004/01/oasis-200401-abc-abcsecurity-xyz- 1.0.xsd">
<SOAP-ENV:Header>
<oas:Security>
<oas:Credential>
<oas:name>%s</oas:name>
<oas:Password>%s</oas:Password>
</oas:Credential>
</oas:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:execute
xmlns:ns1="urn:com.company.us.abc.service.xyz.someService">
<si:ServiceRequestInfo>
<si:Tag1 />
<si:Tag2 />
<si:Tag3 />
<si:Tag4 />
</si:ServiceRequestInfo>
<ns1:ServiceIn>
<ns1:Tag5>%s</ns1:Tag5>
<ns1:File>
<ns1:Sender>
<ns1:Name>%s</ns1:Name>
<ns1:Email>%s</ns1:Email>
<ns1:replyTo>%s</ns1:replyTo>
</ns1:Sender>
</ns1:File>
</ns1:ServiceIn>
</ns1:execute>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>');
update tablename
set XMLColumnname.modify('declare namespace soap="http://schemas.xmlsoap.org/soap/envelope/";
declare namespace foo="urn:com.company.us.abc.service.xyz.someService";
delete /soap:Envelope/soap:Body/foo:execute/foo:ServiceIn/foo:File/foo:Sender/foo:replyTo[1]');
select * from tablename;
Which yields...
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:si="http://someservice.abc.com/stds/xyz/a1"
xmlns:oas="http://docs.oasis-open.org/abc/2004/01/oasis-200401-abc-abcsecurity-xyz- 1.0.xsd">
<SOAP-ENV:Header>
<oas:Security>
<oas:Credential>
<oas:name>%s</oas:name>
<oas:Password>%s</oas:Password>
</oas:Credential>
</oas:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:execute xmlns:ns1="urn:com.company.us.abc.service.xyz.someService">
<si:ServiceRequestInfo>
<si:Tag1 />
<si:Tag2 />
<si:Tag3 />
<si:Tag4 />
</si:ServiceRequestInfo>
<ns1:ServiceIn>
<ns1:Tag5>%s</ns1:Tag5>
<ns1:File>
<ns1:Sender>
<ns1:Name>%s</ns1:Name>
<ns1:Email>%s</ns1:Email>
</ns1:Sender>
</ns1:File>
</ns1:ServiceIn>
</ns1:execute>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

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

Adding node to SQL Server XML is silently failing

Trying to figure out why this snippet does not work.
DECLARE #changeStatus XML, #changeSet XML
SELECT #changeSet = TOP 1 ChangeSet
FROM MyTable
SET #changeStatus = '<change id="' + CAST(NEWID() AS NVARCHAR(36)) + '" by="' + #authorizingUserName + '" byAccountId="' + CAST(#authorizingUserId AS NVARCHAR(36)) + '" when="' + CONVERT(NVARCHAR(50),GETUTCDATE(),127) + 'Z">'
+ '<property id="Status" name="Status" old="' + #status + '" new="Closed" />'
+ '<collections />'
+ '</change>'
-- THIS DOES NOT ERROR OUT AND DOES NOT DO ANYTHING!!
SET #changeSet.modify('insert sql:variable("#changeStatus") as last into (/changes)[1]')
The overall structure of the XML is:
<changes>
<change id="" by="" byAccountId="" when="">
<property />
<collections />
</change>
</changes>
When I run the script and check the #changeSet before and after processing, they are identical. i.e.: The #changeStatus was never added to the XML contained in #changeSet.
If the original was:
<changes>
<change id="change01" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
<change id="change02" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
</changes>
I expected to see:
<changes>
<change id="change01" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
<change id="change02" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
<change id="82ECB3C5-D3BA-4CD2-B62C-89C083E4BAA1" by="me#mydomain.com" byAccountId="1E910737-D78C-E711-9C04-00090FFE0001" when="2018-01-17T00:12:33.700Z">
<property id="Status" name="Status" old="In Review" new="Closed" />
<collections />
</change>
</changes>
Does anyone see what might be wrong?
You should never create an XML on string level!
This has various side-effects.
If one of the variables is NULL the whole string will be NULL
Try SELECT '<x>' + NULL + '</x>';
XML is not just text with some fancy extras. Using T-SQL's FOR XML will - for sure! - create valid XML. Now imagine one of your string contains forbidden characters...
Try SELECT '<x>' + 'The arrow "->" might appear in normal text' + '</x>';
And try to cast it to XML
Many values are not stored in readable format but in some binary format. Depending on your system's settings the conversion of such types (numbers, date and time, BLOBs) will vary. Using FOR XML will use - for sure! - the correct format in and out. You can write anything within your XML, but on another system it might fail to read it.
Try it this way:
DECLARE #changeSet XML = N'<changes/>';
DECLARE #changeStatus XML;
SET #changeStatus=
(
SELECT NEWID() AS [#id]
,'SomeAuthor' AS [#by]
,'SomeAuthorId' AS [#byAccountId]
,GETUTCDATE() AS [#when]
,'Status' AS [property/#id]
,'Status' AS [property/#name]
,'In Review' AS [property/#old]
,'Closed' AS [property/#new]
,'' AS [collections]
FOR XML PATH('change')
);
--your code works fine
SET #changeSet.modify(N'insert sql:variable("#changeStatus") as last into (/changes)[1]');
--you see the first change integrated
SELECT #changeSet;
--Now let us create one more element
SET #changeStatus=
(
SELECT NEWID() AS [#id]
,'Another' AS [#by]
,'OneMore' AS [#byAccountId]
,GETUTCDATE() AS [#when]
,'Status' AS [property/#id]
,'Status' AS [property/#name]
,'In Review' AS [property/#old]
,'Closed' AS [property/#new]
,'' AS [collections]
FOR XML PATH('change')
);
--and insert it
SET #changeSet.modify(N'insert sql:variable("#changeStatus") as last into (/changes)[1]');
--yeah, worked!
SELECT #changeSet;
The query you posted works in general. I ran following test:
http://rextester.com/RNX55555
However, if any of the injected parameters is null, the change will not be inserted and no error will be thrown as you try to insert NULL aka nothing.

Querying XML within SQL Server with namespace

I'm trying to query some XML data that I was sent. I have followed various examples, but can't even address any of the elements beneath the root. I'm not sure what I'm missing. I've tried simply pulling the xml beneath /KitchenSupply/StoreInfo and nothing. Is the namespace the culprit for me not being able to return anything?
declare #myxmlinv as xml =
'<?xml version="1.0" encoding="utf-8"?>
<KitchenSupply xmlns="http://www.starstandards.org/STAR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="KitchenSupply.xsd">
<StoreInfo>
<RCPT>
<SubTaskCode>ZZZ</SubTaskCode>
<SubComponentCode>BS1</SubComponentCode>
<StoreNumber>2241</StoreNumber>
<USRegionNumber>SE</USRegionNumber>
</RCPT>
<SentDateTime>02/04/2015</SentDateTime>
<IntId>ABC1234587</IntId>
<Destinations>
<DestinationFormatType>KS_XML_v3</DestinationFormatType>
</Destinations>
</StoreInfo>
<KitchenOrder>
<KsRecord SalesTransactionPayType="CC" SalesTransactionType="Closed">
<ReceiptAmt RcptTotalAmt="0.00" CustRcptTotalAmt="25.22" CCPostDate="01/07/2015" InvoiceDate="01/05/2015" CustName="JOHN SMITH" CustNo="13998" RcptNo="78476221" />
<KsCosts>
<SaleAmts UnitCost="15.00" TxblAmt="15.00" PayType="Cust" />
<SalesInfo JobStatus="F" JobNo="1" ItemCode="HT093" ItemDesc="Hand Towel">
<EmpInfo EmpRate="16.00" EmpCommPct="1.2" EmpName="DOUG ROGERS" EmpNo="998331" />
</SalesInfo>
</KsCosts>
</KsRecord>
<CustomerRecord>
<ContactInfo LastName="SMITH" FreqFlag="Y">
<Address Zip="90210" State="CA" City="BEV" Addr1="123 MAIN ST" Type="Business" />
<Email MailTo="FAKE#USA.COM" />
<Phone Num="1235551212" Type="H" />
<Phone Num="1235551213" Type="B" />
</ContactInfo>
</CustomerRecord>
</KitchenOrder>
<KitchenOrder>
<KsRecord SalesTransactionPayType="CC" SalesTransactionType="Closed">
<ReceiptAmt RcptTotalAmt="0.00" CustRcptTotalAmt="5.71" CCPostDate="01/08/2015" InvoiceDate="01/07/2015" CustName="SARAH BALDWIN" CustNo="14421" RcptNo="78476242" />
<KsCosts>
<SaleAmts UnitCost="2.00" TxblAmt="2.00" PayType="Cust" />
<SalesInfo JobStatus="F" JobNo="1" ItemCode="HS044" ItemDesc="Hand Soap">
<EmpInfo EmpRate="16.00" EmpCommPct="1.2" EmpName="DOUG ROGERS" EmpNo="998331" />
</SalesInfo>
</KsCosts>
</KsRecord>
<CustomerRecord>
<ContactInfo LastName="BALDWIN" FreqFlag="N">
<Address Zip="90210" State="CA" City="BEV" Addr1="123 VINE ST" Type="Home" />
<Email MailTo="FAKESARAH#USA.COM" />
<Phone Num="1235555512" Type="H" />
<Phone Num="1235556613" Type="M" />
</ContactInfo>
</CustomerRecord>
</KitchenOrder>
</KitchenSupply>';
declare #myxmlinv_table as table (
row_id tinyint,
inv_xml xml
);
insert into #myxmlinv_table(row_id,inv_xml) values('1',#myxmlinv);
Display the XML document and pull the IntId column: (doesn't work)
select i.row_id, i.inv_xml, i.inv_xml.value('(/KitchenSupply/StoreInfo/IntId)[1]','varchar(255)') as data_description
from #myxmlinv_table i
Attempt to use XMLNAMESPACES to display the XML document at the StoreInfo level: (also doesn't work)
WITH XMLNAMESPACES ('http://www.starstandards.org/STAR' as ks)
SELECT
XmlCol.query('/ks:KitchenSupply/StoreInfo')
FROM T;
Ideally, I'd like to use the nodes to extract all the data out and in to separate tables for querying.
KitchenOrders all within one table, CustomerRecord within another, etc.
Any ideas?
You need to use something like this:
-- define the XML namespace as the "default" so you don't have to
-- prefix each and every XPath element with the XML namespace prefix
WITH XMLNAMESPACES( DEFAULT 'http://www.starstandards.org/STAR')
SELECT
-- reach into the <KsRecord> subnode under <KitchenOrder>
ReceiptNo = XC.value('(KsRecord/ReceiptAmt/#RcptNo)[1]', 'int'),
CustName = XC.value('(KsRecord/ReceiptAmt/#CustName)[1]', 'varchar(25)'),
-- reach into the <CustomerRecord> subnode under <KitchenOrder>
CustomerLastName = xc.value('(CustomerRecord/ContactInfo/#LastName)[1]', 'varchar(50)'),
CustomerEmail = xc.value('(CustomerRecord/ContactInfo/Email/#MailTo)[1]', 'varchar(50)')
FROM
-- get a "virtual" table of XML fragments, one for each <KitchenOrder> node
#myxmlinv.nodes('/KitchenSupply/KitchenOrder') AS XT(XC)
Yes, it's the namespace. To get SQL Server to ignore the namespace you can use local-name:
SELECT *
FROM #myxmlinv_table
WHERE inv_xml.exist('//*[local-name()="KitchenSupply"]') = 1

Resources