简体   繁体   English

如何让SQL Server XQUERY返回“没有名为[Element]的元素”之外的其他内容

[英]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' 我一直收到错误消息XQuery [Mytable.XMLData.nodes()]:没有名为'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 我得到结果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: 您没有注意游戏中的XML命名空间:

<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. 您需要使用ns: XML名称空间前缀引用<Answer>内的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM