开发者

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

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

SELECT 
ref.va开发者_JAVA百科lue('/','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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜