简体   繁体   English

如何从SQL中以ntext存储的XML中提取值

[英]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. 我有一个数据库,试图在其中查询在SQL Server中存储为ntext的XML的列。 Why is it not stored as XML? 为什么不将其存储为XML? Not sure but I can't change it so I have worked to try and convert it to some XML format. 不确定,但是我无法更改它,因此我尝试将其转换为XML格式。 At this point I have been able to run this to get rows of entries that are treated as XML. 至此,我已经能够运行它来获取被视为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 这导致以下被识别为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'. 我想提取Request_Category为“信息请求”。

How can I pull these out using XML methods and insert them into two columns in my query so I have Name and Value? 如何使用XML方法提取这些内容并将其插入查询的两列中,以便获得名称和值?

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. 如果有的话,我希望它是一个好的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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