views:

455

answers:

1

Apologies if this is answered elsewhere. I keep getting the error message XQuery [Mytable.XMLData.nodes()]: There is no element named 'Answer'

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've tried several different variations on getting the root 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'd ultimately 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 your help

+1  A: 

You're not paying attention to the XML namespace in play:

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

You need to take that into account when querying - do something like this:

;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)

You need to reference everything inside <Answer> with the ns: XML namespace prefix.

marc_s
Beta033