简体   繁体   中英

Convert VARCHAR column containing XML to table

I am working with VMware vCenter 5 and want to create a quick report of the DRS rules that have been created. Below is an excerpt of the data:

<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="5.0" xsi:type="ArrayOfClusterRuleInfo">
  <ClusterRuleInfo xsi:type="ClusterAffinityRuleSpec">
    <key>1</key>
    <enabled>true</enabled>
    <name>SQL Servers</name>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-100</vm>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-200</vm>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-300</vm>
  </ClusterRuleInfo>
  <ClusterRuleInfo xsi:type="ClusterAntiAffinityRuleSpec">
    <key>2</key>
    <enabled>true</enabled>
    <name>Oracle DBs</name>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-1000</vm>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-2000</vm>
  </ClusterRuleInfo>
</obj>

Note that the number of VMs per rule may differ.

I have not been able to turn this XML data into a table, so I tried locating just the name (within the tags) with this query:

SELECT ref.value('name[1]', 'varchar(200)') as [enabled]
FROM @data.nodes('/obj/ClusterRuleInfo') data( ref )

It returns no data. I found that if I remove the and lines, it works, but I'm not sure why. I believe it is because SQL Server does not understand the XML schema (or something similar - I'm not an XML expert).

In the end, I'd like to see a table like this:

Name           VM
-----------   ----
SQL Servers    100
SQL Servers    200
SQL Servers    300
Oracle DBs    1000
Oracle DBs    2000

Your XML uses a namespace, so you need the WITH XMLNAMESPACES clause:

declare @data xml;
set @data = '
<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="5.0" xsi:type="ArrayOfClusterRuleInfo">
  <ClusterRuleInfo xsi:type="ClusterAffinityRuleSpec">
    <key>1</key>
    <enabled>true</enabled>
    <name>SQL Servers</name>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-100</vm>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-200</vm>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-300</vm>
  </ClusterRuleInfo>
  <ClusterRuleInfo xsi:type="ClusterAntiAffinityRuleSpec">
    <key>2</key>
    <enabled>true</enabled>
    <name>Oracle DBs</name>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-1000</vm>
    <vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-2000</vm>
  </ClusterRuleInfo>
</obj>';

WITH XMLNAMESPACES('urn:vim25' AS ns)
SELECT 
  ref.value('../ns:name[1]','nvarchar(256)') AS Name, 
  STUFF(ref.value('.','nvarchar(256)'),1,3,'') AS VM
FROM @data.nodes('/ns:obj/ns:ClusterRuleInfo/ns:vm') data( ref );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM