1

这个问题看起来真的很恶心。我在名为“消息”的列中有以下小 XML,我想查询它。我遇到的问题是“ClaimData”元素。如您所见,它将其命名空间设置为空字符串。

<DataExchange xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://schemas.abc.com/library/DataExchange">
  <Data>
      <ClaimData xmlns="">
          <CurrentClaimNumber>TEST0000319001</CurrentClaimNumber>
      </ClaimData>
  </Data>
</DataExchange>      

下面的查询返回 XML 列的 NULL 值,因为 ClaimData 元素使用空命名空间,我不知道如何在 XML 列的查询中指出这一点。

有人可以给出一个工作(并希望经过测试)示例,说明如何在 XML 消息列中查询“CurrentClaimNumber”元素(它是“ClaimData”的子元素)?

非常非常感谢。

WITH XMLNAMESPACES (
     'http://www.w3.org/2001/XMLSchema' AS "xsd",
    'http://www.w3.org/2001/XMLSchema-instance' AS "xsi",
     DEFAULT 'http://schemas.rising.com/library/DataExchange'
) SELECT [StoredMessageID],
    Message.value('(/DataExchange/Data/ClaimData/CurrentClaimNumber)[1]', 'CHAR(750)')
  FROM [DataExchange].[dbo].[MessageStorage]
4

1 回答 1

3

将默认命名空间分配给前缀怎么样,这样您就可以在没有前缀的情况下使用空命名空间?这可能是唯一的选择:

WITH XMLNAMESPACES (
     'http://www.w3.org/2001/XMLSchema' AS "xsd",
    'http://www.w3.org/2001/XMLSchema-instance' AS "xsi",
     'http://schemas.abc.com/library/DataExchange' AS "de"
) SELECT [StoredMessageID],
    Message.value('(/de:DataExchange/de:Data/ClaimData/CurrentClaimNumber)[1]', 
                                                                         'CHAR(750)')
  FROM [DataExchange].[dbo].[MessageStorage]

验证如下:

insert into MessageStorage values('
<DataExchange
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://schemas.abc.com/library/DataExchange">
  <Data>
      <ClaimData xmlns="">
          <CurrentClaimNumber>TEST0000319001</CurrentClaimNumber>
      </ClaimData>
  </Data>
</DataExchange>');


WITH XMLNAMESPACES (
     'http://www.w3.org/2001/XMLSchema' AS "xsd",
    'http://www.w3.org/2001/XMLSchema-instance' AS "xsi",
     'http://schemas.abc.com/library/DataExchange' AS "de"
) SELECT [StoredMessageID],
    Message.value('(/de:DataExchange/de:Data/ClaimData/CurrentClaimNumber)[1]', 
                                                                         'CHAR(750)')
  FROM [dbo].[MessageStorage]

结果:

 1     TEST0000319001
于 2013-03-14T03:56:29.360 回答