Sql 为不同的xml结构解析xml

Sql 为不同的xml结构解析xml,sql,sql-server,xml,tsql,Sql,Sql Server,Xml,Tsql,你能给我一些建议吗 我需要将这个xml文本解析成可读的格式 Additional Notes: ??????????????????????????? Business Process ID: 30001 等 这需要在视图中的sql server 2014上的transact-sql中完成 我知道我可以编写一个脚本来提取数据,但是数据(名称和值)每次都会改变,嵌套xml(问题)的结构每次都会不同 有没有办法找到所有的结构,然后提取数据 <AdapterItem xmlns:i="http

你能给我一些建议吗

我需要将这个xml文本解析成可读的格式

Additional Notes: ???????????????????????????
Business Process ID: 30001

这需要在视图中的sql server 2014上的transact-sql中完成

我知道我可以编写一个脚本来提取数据,但是数据(名称和值)每次都会改变,嵌套xml(问题)的结构每次都会不同

有没有办法找到所有的结构,然后提取数据

<AdapterItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://???????????????/??????????/2011-11-18/Data">
  <Attributes>
    <Attribute>
      <Name>Additional Notes</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">???????????????????????????</Value>
    </Attribute>
    <Attribute>
      <Name>Business Process ID</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">30001</Value>
    </Attribute>
    <Attribute>
      <Name>Channel Location</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Br??????????????????es</Value>
    </Attribute>
    <Attribute>
      <Name>Channel Sub Location</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Advisor desk</Value>
    </Attribute>
    <Attribute>
      <Name>Contact Reason</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Preferred method of contact</Value>
    </Attribute>
    <Attribute>
      <Name>Date Submitted</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:dateTime">2017-07-20T16:43:03.5090344+01:00</Value>
    </Attribute>
    <Attribute>
      <Name>Mobile Phone</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">???????????????????</Value>
    </Attribute>
    <Attribute>
      <Name>Service Request Language</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">English (United Kingdom)</Value>
    </Attribute>
    <Attribute>
      <Name>Service Request Type</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">??????????????????????????????</Value>
    </Attribute>
    <Attribute>
      <Name>Submission Channel</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Fa??????????????e</Value>
    </Attribute>
    <Attribute>
      <Name>Wish to Remain Anonymous</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">false</Value>
    </Attribute>
    <Attribute>
      <Name>Questions</Name>
        <Value i:type="AdapterItem">
          <Attributes>
            <Attribute>
              <Name>Contact Group</Name>
              <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:string">CUST</Value>
            </Attribute>
            <Attribute>
              <Name>Created</Name>
              <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:dateTime">2017-07-18T13:28:40.66</Value>
            </Attribute>
          </Attributes>
         </Value
    </Attribute>
    <Attribute>
      <Name>Agent Username</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">m????????e</Value>
    </Attribute>
    <Attribute>
      <Name>Start DateTime</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:dateTime">2017-07-20T16:41:43.833</Value>
    </Attribute>
    <Attribute>
      <Name>Customer</Name>
      <Value i:type="AdapterItem">
        <Attributes>
          <Attribute>
            <Name>Contact Group</Name>
            <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:string">CUST</Value>
          </Attribute>
          <Attribute>
            <Name>Created</Name>
            <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:dateTime">2017-07-18T13:28:40.66</Value>
          </Attribute>
          <Attribute>
            <Name>Created</Name>
            <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:String">jo b</Value>
          </Attribute>
        </Attributes>
      </Value
    </Attribute>
  </Attributes>
</AdapterItem>

附加说明
???????????????????????????
业务流程ID
30001
通道位置
Br
通道子位置
顾问台
联系原因
首选接触方法
提交日期
2017-07-20T16:43:03.5090344+01:00
手机
???????????????????
服务请求语言
英语(联合王国)
服务请求类型
??????????????????????????????
提交频道
法兰西
希望匿名
假的
问题
联络小组
卡斯特
创建
2017-07-18T13:28:40.66

使用下面的方法,您可以将给定的XML分解为列

但是:您的结构看起来可能嵌套到一个不确定的深度(EAV样式的经典BOM表)

给定的方法读取两个级别的深度,您可以轻松添加级别以将其扩展到已知的最大级别。如果没有最大深度,则需要递归方法

试一试:

DECLARE @xml XML=
N'<AdapterItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://???????????????/??????????/2011-11-18/Data">
  <Attributes>
    <Attribute>
      <Name>Additional Notes</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">???????????????????????????</Value>
    </Attribute>
    <Attribute>
      <Name>Business Process ID</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">30001</Value>
    </Attribute>
    <Attribute>
      <Name>Channel Location</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Br??????????????????es</Value>
    </Attribute>
    <Attribute>
      <Name>Channel Sub Location</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Advisor desk</Value>
    </Attribute>
    <Attribute>
      <Name>Contact Reason</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Preferred method of contact</Value>
    </Attribute>
    <Attribute>
      <Name>Date Submitted</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:dateTime">2017-07-20T16:43:03.5090344+01:00</Value>
    </Attribute>
    <Attribute>
      <Name>Mobile Phone</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">???????????????????</Value>
    </Attribute>
    <Attribute>
      <Name>Service Request Language</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">English (United Kingdom)</Value>
    </Attribute>
    <Attribute>
      <Name>Service Request Type</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">??????????????????????????????</Value>
    </Attribute>
    <Attribute>
      <Name>Submission Channel</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Fa??????????????e</Value>
    </Attribute>
    <Attribute>
      <Name>Wish to Remain Anonymous</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">false</Value>
    </Attribute>
    <Attribute>
      <Name>Questions</Name>
        <Value i:type="AdapterItem">
          <Attributes>
            <Attribute>
              <Name>Contact Group</Name>
              <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:string">CUST</Value>
            </Attribute>
            <Attribute>
              <Name>Created</Name>
              <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:dateTime">2017-07-18T13:28:40.66</Value>
            </Attribute>
          </Attributes>
         </Value>
    </Attribute>
    <Attribute>
      <Name>Agent Username</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">m????????e</Value>
    </Attribute>
    <Attribute>
      <Name>Start DateTime</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:dateTime">2017-07-20T16:41:43.833</Value>
    </Attribute>
    <Attribute>
      <Name>Customer</Name>
      <Value i:type="AdapterItem">
        <Attributes>
          <Attribute>
            <Name>Contact Group</Name>
            <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:string">CUST</Value>
          </Attribute>
          <Attribute>
            <Name>Created</Name>
            <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:dateTime">2017-07-18T13:28:40.66</Value>
          </Attribute>
          <Attribute>
            <Name>Created</Name>
            <Value xmlns:d7p1="http://www.w3.org/2001/XMLSchema" i:type="d7p1:String">jo b</Value>
          </Attribute>
        </Attributes>
      </Value>
    </Attribute>
  </Attributes>
</AdapterItem>';
结果

+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| TopAttrName              | TopAttrType   | TopAttrValue                      | L2AttrName    | L2AttrType    | L2AttrValue            |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Additional Notes         | d4p1:string   | ???????????????????????????       | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Business Process ID      | d4p1:string   | 30001                             | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Channel Location         | d4p1:string   | Br??????????????????es            | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Channel Sub Location     | d4p1:string   | Advisor desk                      | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Contact Reason           | d4p1:string   | Preferred method of contact       | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Date Submitted           | d4p1:dateTime | 2017-07-20T16:43:03.5090344+01:00 | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Mobile Phone             | d4p1:string   | ???????????????????               | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Service Request Language | d4p1:string   | English (United Kingdom)          | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Service Request Type     | d4p1:string   | ??????????????????????????????    | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Submission Channel       | d4p1:string   | Fa??????????????e                 | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Wish to Remain Anonymous | d4p1:string   | false                             | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Questions                | AdapterItem   | NULL                              | Contact Group | d7p1:string   | CUST                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Questions                | AdapterItem   | NULL                              | Created       | d7p1:dateTime | 2017-07-18T13:28:40.66 |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Agent Username           | d4p1:string   | m????????e                        | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Start DateTime           | d4p1:dateTime | 2017-07-20T16:41:43.833           | NULL          | NULL          | NULL                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Customer                 | AdapterItem   | NULL                              | Contact Group | d7p1:string   | CUST                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Customer                 | AdapterItem   | NULL                              | Created       | d7p1:dateTime | 2017-07-18T13:28:40.66 |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
| Customer                 | AdapterItem   | NULL                              | Created       | d7p1:String   | jo b                   |
+--------------------------+---------------+-----------------------------------+---------------+---------------+------------------------+
更新:递归方法 尝试以下方法获得独立深度:

WITH recCTE AS
(
    SELECT 1 AS NestLevel
          ,CAST(NULL AS UNIQUEIDENTIFIER) AS ParentAttribute
          ,NEWID() AS AttributeKey
          ,A.TopAttr.value(N'(*:Name/text())[1]',N'nvarchar(max)') AS AttrName
          ,A.TopAttr.value(N'(*:Value/@*:type)[1]',N'nvarchar(max)') AS AttrType
          ,A.TopAttr.value(N'(*:Value/text())[1]',N'nvarchar(max)') AS AttrValue
          ,A.TopAttr.query(N'*:Value/*:Attributes/*:Attribute') AS NextLevel
    FROM @xml.nodes(N'/*:AdapterItem/*:Attributes/*:Attribute') AS A(TopAttr)

    UNION ALL

    SELECT r.NestLevel+1
          ,r.AttributeKey
          ,NEWID()
          ,B.NextAttr.value(N'(*:Name/text())[1]',N'nvarchar(max)') 
          ,B.NextAttr.value(N'(*:Value/@*:type)[1]',N'nvarchar(max)') 
          ,B.NextAttr.value(N'(*:Value/text())[1]',N'nvarchar(max)') 
          ,B.NextAttr.query(N'*:Value/*:Attributes/*:Attribute') 
    FROM recCTE AS r
    OUTER APPLY r.NextLevel.nodes(N'*:Attribute') AS B(NextAttr)
    WHERE NextLevel.exist('*:Attribute')=1
)
SELECT * FROM recCTE;
该查询以级别1属性作为锚点开始,并传递(如果有)较低级别作为
query()
,这是较深节点的片段。只要有更多属性,递归部分就会向下遍历


guid用于保持父子关系。

非常感谢,您的怀疑是正确的,这需要递归,因为我不知道级别。上的每一个都可能是不同的。我可以运行顶层,然后每个项目都有一个
AdaptorItem
类型,然后我可以在游标中查询。我正在试图弄清楚如何从@data.nodes(N'/*:AdapterItem/*:Attributes/*:Attribute[名称='Customer']/*:Attributes/*:Attributes')中查询特定级别的topatrtrValue(N'(*:value/text())[1]、N'nvarchar(max')@Easty避免
光标
如果可能的话。。。查看我的更新
WITH recCTE AS
(
    SELECT 1 AS NestLevel
          ,CAST(NULL AS UNIQUEIDENTIFIER) AS ParentAttribute
          ,NEWID() AS AttributeKey
          ,A.TopAttr.value(N'(*:Name/text())[1]',N'nvarchar(max)') AS AttrName
          ,A.TopAttr.value(N'(*:Value/@*:type)[1]',N'nvarchar(max)') AS AttrType
          ,A.TopAttr.value(N'(*:Value/text())[1]',N'nvarchar(max)') AS AttrValue
          ,A.TopAttr.query(N'*:Value/*:Attributes/*:Attribute') AS NextLevel
    FROM @xml.nodes(N'/*:AdapterItem/*:Attributes/*:Attribute') AS A(TopAttr)

    UNION ALL

    SELECT r.NestLevel+1
          ,r.AttributeKey
          ,NEWID()
          ,B.NextAttr.value(N'(*:Name/text())[1]',N'nvarchar(max)') 
          ,B.NextAttr.value(N'(*:Value/@*:type)[1]',N'nvarchar(max)') 
          ,B.NextAttr.value(N'(*:Value/text())[1]',N'nvarchar(max)') 
          ,B.NextAttr.query(N'*:Value/*:Attributes/*:Attribute') 
    FROM recCTE AS r
    OUTER APPLY r.NextLevel.nodes(N'*:Attribute') AS B(NextAttr)
    WHERE NextLevel.exist('*:Attribute')=1
)
SELECT * FROM recCTE;