简体   繁体   中英

How to extract values from XML stored as ntext in SQL

I have a database where I am trying to query a column where there is XML stored as ntext in our SQL Server. Why is it not stored as XML? Not sure but I can't change it so I have worked to try and convert it to some XML format. At this point I have been able to run this to get rows of entries that are treated as XML.

select  top 5 cast(cast(ATTR_XML as varchar(max)) as XML)
from dbo.table1

This resulted in the following that is recognized as XML

<ArrayOfNameValue xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <NameValue>
    <Name>Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5</Name>
    <Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34</Value>
  </NameValue>
  <NameValue>
    <Name>Form</Name>
    <Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
  </NameValue>
  <NameValue>
    <Name>Form_Update Request_E4FA8434326E42ADB6222978214E93E9</Name>
    <Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
  </NameValue>
  <NameValue>
    <Name>//</Name>
    <Value xsi:type="xsd:string">&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;pd:Data xmlns:pd="http://www.test.com/XMLSchema"&gt;&lt;pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue=""&gt;Coastal Urban&lt;/pd:Request_Source&gt;&lt;pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue=""&gt;Information Request&lt;/pd:Request_Category&gt;&lt;pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue=""&gt;05 Feb 2015&lt;pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue=""&gt;06-Feb-15 03:38:58&lt;/pd:Request_Date&gt;&lt;pd:Request_Priority system_name="" datatype="" listID="" listValue=""&gt;&lt;/pd:Request_Priority&gt;&lt;/pd:Data&gt;</Value>
  </NameValue>
</ArrayOfNameValue>

I am trying to extract the element name and value from this

<Value xsi:type="xsd:string">&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;pd:Data xmlns:pd="http://www.test.com/XMLSchema"&gt;&lt;pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue=""&gt;Coastal Urban&lt;/pd:Request_Source&gt;&lt;pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue=""&gt;Information Request&lt;/pd:Request_Category&gt;&lt;pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue=""&gt;05 Feb 2015&lt;pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue=""&gt;06-Feb-15 03:38:58&lt;/pd:Request_Date&gt;&lt;pd:Request_Priority system_name="" datatype="" listID="" listValue=""&gt;&lt;/pd:Request_Priority&gt;&lt;/pd:Data&gt;</Value>

I'd like to extract Request_Category is 'Information Request'.

How can I pull these out using XML methods and insert them into two columns in my query so I have Name and Value?

Thanks

DECLARE @XML XML =
'<ArrayOfNameValue xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <NameValue>
    <Name>Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5</Name>
    <Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34</Value>
  </NameValue>
  <NameValue>
    <Name>Form</Name>
    <Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
  </NameValue>
  <NameValue>
    <Name>Form_Update Request_E4FA8434326E42ADB6222978214E93E9</Name>
    <Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
  </NameValue>
  <NameValue>
    <Name>//</Name>
    <Value xsi:type="xsd:string">&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;pd:Data xmlns:pd="http://www.test.com/XMLSchema"&gt;&lt;pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue=""&gt;Coastal Urban&lt;/pd:Request_Source&gt;&lt;pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue=""&gt;Information Request&lt;/pd:Request_Category&gt;&lt;pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue=""&gt;05 Feb 2015&lt;pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue=""&gt;06-Feb-15 03:38:58&lt;/pd:Request_Date&gt;&lt;pd:Request_Priority system_name="" datatype="" listID="" listValue=""&gt;&lt;/pd:Request_Priority&gt;&lt;/pd:Data&gt;</Value>
  </NameValue>
</ArrayOfNameValue>'; 

SELECT
bar.value('local-name(.)','VARCHAR(10)') as ColName,  
bar.value('./.','VARCHAR(MAX)')  as Value 
FROM
@xml.nodes('/ArrayOfNameValue/NameValue/*') AS foo(bar)

Results:

ColName    Value
---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Name       Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5
Value      ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34
Name       Form
Value      ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69
Name       Form_Update Request_E4FA8434326E42ADB6222978214E93E9
Value      ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69
Name       //
Value      <?xml version="1.0" encoding="utf-8"?><pd:Data xmlns:pd="http://www.test.com/XMLSchema"><pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue="">Coastal Urban</pd:Request_Source><pd:Request_Category system_name="ojZD66m94eu" da

Is this what you were looking for? If anything, I hope its a good start.

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