在SQL Server中使用Nodes方法没有获得XML Parsed值?

时间:2015-08-14 08:10:46

标签: sql sql-server xml sql-server-2008 sql-server-2008-r2

我正在尝试解析并使用以下代码获取xml以下的值,但是我无法获得返回空值或NULL值的值。

我的xml解析代码中缺少什么?

   declare @AddressXML as xml = '<Address>
  <Details FkTypeOfAddress="1" Address1="gh" Address2="yu" Address3="gh" Landmark="gh" City="Kolkata" Pincode="777777" State="ARU" />
  <Details FkTypeOfAddress="2" Address1="io" Address2="io" Address3="io" Landmark="io" City="Kolkata" Pincode="999999" State="GOA" />
</Address>'

SELECT    [Table].[Column].value('(FkTypeOfAddress) [1]','BIGINT') as FkTypeOfAddress
                        ,[Table].[Column].value('(Address1) [1]','varchar(100)') as Address1
                        ,[Table].[Column].value('(Address2) [1]','varchar(100)') as Address2 
                        ,[Table].[Column].value('(Address3) [1]','varchar(100)') as Address3
                        ,[Table].[Column].value('(Landmark) [1]','varchar(200)') as Landmark
                        ,[Table].[Column].value('(City) [1]','varchar(100)') as City
                        ,[Table].[Column].value('(Pincode) [1]','varchar(10)') as Pincode
                        ,[Table].[Column].value('(State) [1]','varchar(50)') as [State]                     
               FROM      @AddressXML.nodes('/Address/Details') as  [Table]([Column]);

enter image description here

2 个答案:

答案 0 :(得分:3)

在XPath / XQuery中,您应该在属性名称的开头使用@来引用属性:

SELECT
    [Table].[Column].value('@FkTypeOfAddress','BIGINT') as FkTypeOfAddress
    ,[Table].[Column].value('@Address1','varchar(100)') as Address1
    ,[Table].[Column].value('@Address2','varchar(100)') as Address2 
    ,[Table].[Column].value('@Address3','varchar(100)') as Address3
    ,[Table].[Column].value('@Landmark','varchar(200)') as Landmark
    ,[Table].[Column].value('@City','varchar(100)') as City
    ,[Table].[Column].value('@Pincode','varchar(10)') as Pincode
    ,[Table].[Column].value('@State','varchar(50)') as [State]                     
FROM @AddressXML.nodes('/Address/Details') as  [Table]([Column]);

答案 1 :(得分:0)

经过小小的智能护目镜后,我找到了解决方案。

declare @AddressXML as xml = '<Address>
  <Details FkTypeOfAddress="1" Address1="gh" Address2="yu" Address3="gh" Landmark="gh" City="Kolkata" Pincode="777777" State="ARU" />
  <Details FkTypeOfAddress="2" Address1="io" Address2="io" Address3="io" Landmark="io" City="Kolkata" Pincode="999999" State="GOA" />
</Address>'

SELECT    [Table].[Column].value('(@FkTypeOfAddress) [1]','BIGINT') as FkTypeOfAddress
                        ,[Table].[Column].value('(@Address1) [1]','varchar(100)') as Address1
                        ,[Table].[Column].value('(@Address2) [1]','varchar(100)') as Address2 
                        ,[Table].[Column].value('(@Address3) [1]','varchar(100)') as Address3
                        ,[Table].[Column].value('(@Landmark) [1]','varchar(200)') as Landmark
                        ,[Table].[Column].value('(@City) [1]','varchar(100)') as City
                        ,[Table].[Column].value('(@Pincode) [1]','varchar(10)') as Pincode
                        ,[Table].[Column].value('(@State) [1]','varchar(50)') as [State]                        
               FROM      @AddressXML.nodes('/Address/Details') as  [Table]([Column]);