How to force SQL Server XQUERY to return something other than "There is no element named [Element]"

Sorry if this is answered elsewhere. I keep getting the XQuery error message [Mytable.XMLData.nodes ()]: There is no item named "Response"

SELECT 
ref.value('/','nvarchar(1000)')
FROM   Mytable CROSS APPLY xmldata.nodes('Answer') R(ref)

-

--XML of Row
<Answer xmlns="http://TempNameSpace.com/AnswerData.xsd" Type="Deliverable">
  <Deliverable>
    <Title>test</Title>
    <Description>test</Description>
    <DueDate>2010-02-16T08:59:59</DueDate>
  </Deliverable>
</Answer>

I tried several different options for getting the root of the node ("answer") or any of the child nodes if, however, I change my statement to read

SELECT 
ref.value('/','nvarchar(1000)')
FROM   Mytable CROSS APPLY xmldata.nodes('/') R(ref)

I get the result testtest2010-02-16T08: 59: 59

I will eventually like this data in tabular format, something like

SELECT 
    ref.value('/Title','nvarchar(1000)') as Title
    ref.value('/Description','nvarchar(1000)') as Description

etc..
    FROM   Mytable CROSS APPLY xmldata.nodes('/Deliverable') R(ref)

thanks for the help

+3
source share
1 answer

You do not pay attention to the XML namespace in the game:

<Answer xmlns="http://TempNameSpace.com/AnswerData.xsd" Type="Deliverable" 
        **********************************************

- - :

;WITH XMLNAMESPACES('http://TempNameSpace.com/AnswerData.xsd' AS ns)
SELECT 
  ref.value('(ns:*)[1]', 'nvarchar(1000)')
FROM Mytable 
CROSS APPLY xmldata.nodes('/ns:Answer') R(ref)

<Answer> ns: XML.

+4

Source: https://habr.com/ru/post/1734956/


All Articles