2017-08-17 84 views
0

我已經使用SQL中的名稱空間解析了XML,但是我有一個真正的熊。我試圖從XML中提取IP地址,但沒有運氣。我認爲它與xsd命名空間有關,但似乎無法找到命名空間聲明和節點路徑的正確組合。在SQL Server 2012中使用T-SQL解析XML

這裏是XML的一個樣本:

<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="6.0" xsi:type="ArrayOfGuestStackInfo"> 
    <GuestStackInfo xsi:type="GuestStackInfo"> 
    <dnsConfig> 
     <dhcp>false</dhcp> 
     <hostName>SXVCUP05</hostName> 
     <domainName>corp.dtcc.com</domainName> 
     <ipAddress>172.18.18.13</ipAddress> 
     <ipAddress>172.18.18.20</ipAddress> 
     <ipAddress>172.22.43.132</ipAddress> 
     <ipAddress>172.22.43.148</ipAddress> 
     <ipAddress>172.22.37.68</ipAddress> 
     <ipAddress>172.22.37.84</ipAddress> 
     <searchDomain>corp.dtcc.com</searchDomain> 
     <searchDomain>dtcc.com</searchDomain> 
     <searchDomain>backup.dtcc.com</searchDomain> 
    </dnsConfig> 
    <ipRouteConfig> 
     <ipRoute> 
     <network>0.0.0.0</network> 
     <prefixLength>0</prefixLength> 
     <gateway> 
      <ipAddress>172.28.224.1</ipAddress> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.18.8.133</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <ipAddress>172.28.224.10</ipAddress> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.22.8.14</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <ipAddress>172.28.224.10</ipAddress> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.28.224.0</network> 
     <prefixLength>24</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.28.224.112</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.28.224.255</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>224.0.0.0</network> 
     <prefixLength>4</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>255.255.255.255</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
    </ipRouteConfig> 
    </GuestStackInfo> 
</obj> 

使用下面的代碼來解析XML

WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema' as xsd) 
    SELECT a.b.value('ipAddress[1]', 'varchar(100)') AS ipaddress 
    FROM @sam_xml.nodes('/obj/GuestStackInfo/dnsConfig') a(b) 

謝謝!

+0

''的所有孩子都在命名空間'xmlns =「urn:vim25」'中。 – zx485

回答

2

您正在查找的值是存在於默認命名空間xmlns中。這是你需要聲明的唯一命名空間:

DECLARE @sam_xml XML= 
N'<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="6.0" xsi:type="ArrayOfGuestStackInfo"> 
    <GuestStackInfo xsi:type="GuestStackInfo"> 
    <dnsConfig> 
     <dhcp>false</dhcp> 
     <hostName>SXVCUP05</hostName> 
     <domainName>corp.dtcc.com</domainName> 
     <ipAddress>172.18.18.13</ipAddress> 
     <ipAddress>172.18.18.20</ipAddress> 
     <ipAddress>172.22.43.132</ipAddress> 
     <ipAddress>172.22.43.148</ipAddress> 
     <ipAddress>172.22.37.68</ipAddress> 
     <ipAddress>172.22.37.84</ipAddress> 
     <searchDomain>corp.dtcc.com</searchDomain> 
     <searchDomain>dtcc.com</searchDomain> 
     <searchDomain>backup.dtcc.com</searchDomain> 
    </dnsConfig> 
    <ipRouteConfig> 
     <ipRoute> 
     <network>0.0.0.0</network> 
     <prefixLength>0</prefixLength> 
     <gateway> 
      <ipAddress>172.28.224.1</ipAddress> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.18.8.133</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <ipAddress>172.28.224.10</ipAddress> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.22.8.14</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <ipAddress>172.28.224.10</ipAddress> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.28.224.0</network> 
     <prefixLength>24</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.28.224.112</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>172.28.224.255</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>224.0.0.0</network> 
     <prefixLength>4</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
     <ipRoute> 
     <network>255.255.255.255</network> 
     <prefixLength>32</prefixLength> 
     <gateway> 
      <device>0</device> 
     </gateway> 
     </ipRoute> 
    </ipRouteConfig> 
    </GuestStackInfo> 
</obj>'; 

--the查詢將使用.nodes()下潛更深層次的原因則您的查詢,以便從<dnsConfig>內得到所有的IP地址:

WITH XMLNAMESPACES(DEFAULT 'urn:vim25') 
SELECT a.b.value('text()[1]', 'varchar(100)') as ipaddress 
FROM @sam_xml.nodes('/obj/GuestStackInfo/dnsConfig/ipAddress') a(b) 
+0

拍你打我一分鐘,哈哈。 – djangojazz

+0

謝謝你的工作! – DeeBeeEh