我已經使用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)
謝謝!
''的所有孩子都在命名空間'xmlns =「urn:vim25」'中。 –
zx485