5

我刚开始在 SQL Server 数据库中查询 XML。我在最基本的查询方面遇到了麻烦。这是一个简化的例子。如何返回描述?下面的 SELECT 语句是我正在使用的,但它什么也不返回。

SELECT Incidents.IncidentXML.query
('data(/dsIncident/IncidentInformation/Description)') AS Description 
FROM Incidents

这是我正在使用的 XML 文件的片段:

<dsIncident xmlns="http://tempuri.org/dsIncident.xsd">
  <IncidentInformation>
    <Description>This is the description.</Description>
    <Country>Singapore</Country>
  </IncidentInformation>
</dsIncident>
4

1 回答 1

13

好吧,您错过了 XML 命名空间!:-)

试试这个:

SELECT 
  Incidents.IncidentXML.query('declare namespace x="http://tempuri.org/dsIncident.xsd";
          (/x:dsIncident/x:IncidentInformation/x:Description)') AS Description 
FROM Incidents

魔法是

declare namespace x="http://tempuri.org/dsIncident.xsd"

部分在这里 - 它声明了一个命名空间(带有您选择的前缀 - 可以是任何东西 - 这里是“x”),用于查询该 XML 数据的期间。

希望这会有所回报!;-)

马克

于 2009-08-19T19:28:36.823 回答