I am newbie to SQL and XML. My aim is to get the data from all the elements within the Client block but the SELECT not returning any data. Without the namespace the query works.
Can you help?
DECLARE @x XML='<BusinessEvent Name="FO.Client">
<ClientMessage xmlns="http://www.fwbs.net/Aderant.BO.Integration/FO.MessageTypes.xsd">
<OriginatingProcessIdentity></OriginatingProcessIdentity>
<Operation></Operation>
<IsDataIncluded></IsDataIncluded>
<Clients>
<Client>
<GlobalId></GlobalId>
<ClID>g</ClID>
<No></No>
<Type></Type>
<Name></Name>
<BrCode></BrCode>
<Created></Created>
<FeeUsrID></FeeUsrID>
<DefaultAddID></DefaultAddID>
<ContID></ContID>
</Client>
</Clients>
</ClientMessage>
</BusinessEvent>'
;WITH XMLNAMESPACES
(DEFAULT 'http://www.fwbs.net/Aderant.BO.Integration/FO.MessageTypes.xsd')
SELECT TOP 10 cl.c.value('(text())[1]', 'varchar(20)') clid
FROM (SELECT @x data) data
CROSS APPLY [data].nodes('/BusinessEvent/ClientMessage/Clients/Client/ClID') AS cl(c)
DEFAULTwas OK, if you changed nothing but....nodes('/*:BusinessEvent..., to wildcard the namespace of the outer-most node.) Just one hint: As you want to get the data from all the elements within the Client block, you should stop theXPathin.nodes()after client to read each element withc.value('ClID[1]','nvarchar(max)'). This allows you to address all inner elements with the sameCROSS APPLY.