SQL从xml解析多个字段

问题描述 投票:0回答:1

我需要帮助,从链接的服务器中提取xml数据,并且仅以表格格式返回某些字段。理想情况下,我想用tableA(term,C,D,F)填充本地计算机表。您会注意到某些节点没有C,D或F,因此我需要排除这些节点。我已经尝试了很多事情,而且似乎语法还不太正确。我的麻烦是从远程服务器中提取并仅解析所需的xml节点。感谢您的帮助。

Running on the remote server:
DECLARE @xml XML
SET @xml = (select top 5 term_uid, field1, term_attributes.query('/') from server.dbo.tableName with (nolock) for xml auto, root('Attributes')
       )
SELECT @xml 

Results:  
<Attributes>
  <server.context.tableName term_uid="7D54155E-75E8-4BBC-9112-0002C70FC781" field1="I71.0">
    <Attributes xmlns="">
      <Attribute name="A">3</Attribute>
      <Attribute name="B">HHS_HCC154</Attribute>
      <Attribute name="C">I71.0</Attribute>
      <Attribute name="D">I71.00</Attribute>
      <Attribute name="F">308546005</Attribute>
    </Attributes>
  </server.context.tablName>
  <server.context.tablName term_uid="1C572AF2-CD26-4C41-B449-0004FF5F4E6E" field1="">
    <Attributes xmlns="">
      <Attribute name="A">2131</Attribute>
      <Attribute name="F">20013001</Attribute>
    </Attributes>
  </server.context.tablName>
  <server.context.tablName term_uid="744A553A-0C2A-4FD7-8004-000519EFAC75" field1="">
    <Attributes xmlns="">
       <Attribute name="D">H66.001</Attribute>
      <Attribute name="F">14948001</Attribute>
    </Attributes>
  </server.context.tablName>
  <server.context.tablName term_uid="5E70C463-4489-4DA7-B410-00082E8E68C5" field1="">
    <Attributes xmlns="">
      <Attribute name="D">S62.353S</Attribute>
      <Attribute name="F">704024008</Attribute>
    </Attributes>
  </server.context.tablName>
  <server.context.tablName term_uid="EEDBCC0B-75B9-47D9-8E7A-000A4B604135" field1="R97.2">
    <Attributes xmlns="">
      <Attribute name="A">3</Attribute>
      <Attribute name="C">R97.2</Attribute>
      <Attribute name="D">790.93</Attribute>
      <Attribute name="F">396152005</Attribute>
    </Attributes>
  </server.context.tablName>
</Attributes>

Also tried doing something like this:
;with listAll as 
    (select term_uid
    ,C= AttributeXML.value('/Attributes/server.context.tableName/Attributes/Attribute/[@name=sql:variable("@C")]/text())[1]','varchar(100')
    ,D= AttributeXML.value('/Attributes/server.context.tableName/Attributes/Attribute/[@name=sql:variable("@D")]/text())[1]','varchar(100') 
    ,F= AttributeXML.value('/Attributes/server.context.tableName/Attributes/Attribute/[@name=sql:variable("@F")]/text())[1]','varchar(100')  
        from server.context.tableName 
        cross apply
            AttributeXML.nodes('/Attributes/server.context.tableName/Attributes/Attribute') as XTbl(XProp)
)
select * from listAll
sql xml linked-server
1个回答
1
投票

检查出来。 XPath谓词表达式正在检查所有三个强制属性的存在:C,D和F。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (term_uid VARCHAR(50) PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (term_uid, xmldata)
VALUES
('7D54155E-75E8-4BBC-9112-0002C70FC781',
N'<Attributes>
    <server.context.tablName term_uid="7D54155E-75E8-4BBC-9112-0002C70FC781" field1="I71.0">
        <Attributes xmlns="">
            <Attribute name="A">3</Attribute>
            <Attribute name="B">HHS_HCC154</Attribute>
            <Attribute name="C">I71.0</Attribute>
            <Attribute name="D">I71.00</Attribute>
            <Attribute name="F">308546005</Attribute>
        </Attributes>
    </server.context.tablName>
    <server.context.tablName term_uid="1C572AF2-CD26-4C41-B449-0004FF5F4E6E" field1="">
        <Attributes xmlns="">
            <Attribute name="A">2131</Attribute>
            <Attribute name="F">20013001</Attribute>
        </Attributes>
    </server.context.tablName>
    <server.context.tablName term_uid="744A553A-0C2A-4FD7-8004-000519EFAC75" field1="">
        <Attributes xmlns="">
            <Attribute name="D">H66.001</Attribute>
            <Attribute name="F">14948001</Attribute>
        </Attributes>
    </server.context.tablName>
    <server.context.tablName term_uid="5E70C463-4489-4DA7-B410-00082E8E68C5" field1="">
        <Attributes xmlns="">
            <Attribute name="D">S62.353S</Attribute>
            <Attribute name="F">704024008</Attribute>
        </Attributes>
    </server.context.tablName>
    <server.context.tablName term_uid="EEDBCC0B-75B9-47D9-8E7A-000A4B604135" field1="R97.2">
        <Attributes xmlns="">
            <Attribute name="A">3</Attribute>
            <Attribute name="C">R97.2</Attribute>
            <Attribute name="D">790.93</Attribute>
            <Attribute name="F">396152005</Attribute>
        </Attributes>
    </server.context.tablName>
</Attributes>');
-- DDL and sample data population, end

SELECT term_uid
    , p.value('@term_uid','VARCHAR(30)') AS term
    , c.value('(Attribute[@name="C"]/text())[1]','VARCHAR(10)') AS c
    , c.value('(Attribute[@name="D"]/text())[1]','VARCHAR(10)') AS d
    , c.value('(Attribute[@name="F"]/text())[1]','VARCHAR(10)') AS f
FROM @tbl AS tbl
    CROSS APPLY tbl.xmldata.nodes('/Attributes/server.context.tablName') AS parent(p)
    CROSS APPLY p.nodes('Attributes[Attribute[@name="C"] 
        and Attribute[@name="D"] 
        and Attribute[@name="F"]]') AS child(c);

输出

+--------------------------------+-------+--------+-----------+
|              term              |   c   |   d    |     f     |
+--------------------------------+-------+--------+-----------+
| 7D54155E-75E8-4BBC-9112-0002C7 | I71.0 | I71.00 | 308546005 |
| EEDBCC0B-75B9-47D9-8E7A-000A4B | R97.2 | 790.93 | 396152005 |
+--------------------------------+-------+--------+-----------+
© www.soinside.com 2019 - 2024. All rights reserved.