2017-06-23 71 views
1

我的数据库需要存储的XML请求时,用户发送以及XML有2个通信元件,WPSA,但他们没有固定的(我的意思是位置):MSSQL查询XML差异为了

<bms:OrgInfo> 
     <bms:Communications> 
     <bms:CommQualifier>WP</bms:CommQualifier> 
     <bms:CommPhone>5555551212</bms:CommPhone> 
     </bms:Communications> 
     <bms:Communications> 
     <bms:CommQualifier>SA</bms:CommQualifier> 
     <bms:Address> 
      <bms:Address1>1234 Test Avenue</bms:Address1> 
      <bms:Address2>1234 Test Avenue</bms:Address2> 
      <bms:City>Alamogordo</bms:City> 
      <bms:StateProvince>NM</bms:StateProvince> 
      <bms:PostalCode>88310</bms:PostalCode> 
      <bms:CountryCode>US</bms:CountryCode> 
     </bms:Address> 
     </bms:Communications> 
    </bms:OrgInfo> 

有时候SA元素将是第一个。

如何查询它不用担心poistion

这里是我当前的查询,我使用的索引进行查询:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[XmlStore]') AND type in (N'U')) 
DROP TABLE [dbo].[XmlStore] 
GO 

CREATE TABLE [dbo].[XmlStore](
    [XmlRequest] [xml] NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

INSERT INTO [XmlStore]([XmlRequest]) VALUES('<?xml version="1.0" encoding="UTF-8"?> 
    <bms:OrgInfo xmlns:bms="http://example.org"> 
     <bms:Communications> 
     <bms:CommQualifier>WP</bms:CommQualifier> 
     </bms:Communications> 
     <bms:Communications> 
     <bms:CommQualifier>SA</bms:CommQualifier> 
     </bms:Communications> 
     <bms:Communications> 
     <bms:CommQualifier>EM</bms:CommQualifier> 
     </bms:Communications> 
    </bms:OrgInfo>') 


;WITH xmlnamespaces ('http://example.org' AS bms) 
Select 
    t.XmlRequest.value('(/bms:OrgInfo/bms:Communications/bms:CommQualifier)[1]', 'nvarchar(100)') as WP_Value, 
    t.XmlRequest.value('(/bms:OrgInfo/bms:Communications/bms:CommQualifier)[2]', 'nvarchar(100)') as SA_Value, 
    t.XmlRequest.value('(/bms:OrgInfo/bms:Communications/bms:CommQualifier)[3]', 'nvarchar(100)') as EM_Value 
FROM [XmlStore] AS t  

DROP TABLE [dbo].[XmlStore] 

这将是一个灾难,如果用户重新排序通讯元素!

+0

我们可以看到你的脚本吗? – maSTAShuFu

+0

@maSTAShuFu:是的,我编辑帖子更新脚本 –

回答

4

您可以添加XQuery predicate喜欢这里

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[XmlStore]') AND type in (N'U')) 
DROP TABLE [dbo].[XmlStore] 
GO 

CREATE TABLE [dbo].[XmlStore](
    [XmlRequest] [xml] NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

INSERT INTO [XmlStore]([XmlRequest]) VALUES('<?xml version="1.0" encoding="UTF-8"?> 
    <bms:OrgInfo xmlns:bms="http://example.org"> 
     <bms:Communications> 
     <bms:CommQualifier>WP</bms:CommQualifier> 
     </bms:Communications> 
     <bms:Communications> 
     <bms:CommQualifier>SA</bms:CommQualifier> 
     </bms:Communications> 
     <bms:Communications> 
     <bms:CommQualifier>EM</bms:CommQualifier> 
     </bms:Communications> 
    </bms:OrgInfo>') 

--The XML一次,但以不同的顺序

INSERT INTO [XmlStore]([XmlRequest]) VALUES('<?xml version="1.0" encoding="UTF-8"?> 
    <bms:OrgInfo xmlns:bms="http://example.org"> 
     <bms:Communications> 
     <bms:CommQualifier>SA</bms:CommQualifier> 
     </bms:Communications> 
     <bms:Communications> 
     <bms:CommQualifier>EM</bms:CommQualifier> 
     </bms:Communications> 
     <bms:Communications> 
     <bms:CommQualifier>WP</bms:CommQualifier> 
     </bms:Communications> 
    </bms:OrgInfo>') 

--The查询(所有指标均[1]

;WITH xmlnamespaces ('http://example.org' AS bms) 
Select 
    t.XmlRequest.value('(/bms:OrgInfo/bms:Communications[bms:CommQualifier="WP"]/bms:CommQualifier)[1]', 'nvarchar(100)') as WP_Value, 
    t.XmlRequest.value('(/bms:OrgInfo/bms:Communications[bms:CommQualifier="SA"]/bms:CommQualifier)[1]', 'nvarchar(100)') as SA_Value, 
    t.XmlRequest.value('(/bms:OrgInfo/bms:Communications[bms:CommQualifier="EM"]/bms:CommQualifier)[1]', 'nvarchar(100)') as EM_Value 
FROM [XmlStore] AS t  

DROP TABLE [dbo].[XmlStore] 
+0

谢谢,它的工作 –