XQuery 检索值

XQuery to retrieve value

我有这个 XML,我正在尝试从中检索值...

<ExportPartyAddressingResponse xmlns="http://www.ediel.no/Export">
  <CompanyListMessage>
    <Header GeneratedAt="2021-11-11T05:00:02Z" SenderApplication="www.ediel.se" />
    <Market Code="EL" CountryCode="SE">
      <Company>
        <Name>7H Kraft Energiförmedling AB</Name>
        <ContactInformation>
          <Item Type="Telephone">+46321532323</Item>
          <Item Type="Telefax">+46321532321</Item>
          <Item Type="Web">www.7hkraft.se</Item>
        </ContactInformation>
        <Identifiers>
          <Key Type="EdielId">60900</Key>
          <Key Type="OrgNo">556525-8075</Key>
          <Key Type="SvKId">7HK</Key>
        </Identifiers>
        <Addresses>
          <Address Type="Postal">
            <Address1>Box 25</Address1>
            <PostCode>22100</PostCode>
            <Place>LUND</Place>
            <CountryCode>SE</CountryCode>
          </Address>
        </Addresses>
        <Roles>
          <Role>PowerSupplier</Role>
        </Roles>
        <EdielAddressing>
          <EDIFACTAddressing>
            <EDIFACTDetails Type="PRODAT">
              <EDICharset>UNOC</EDICharset>
              <EDISyntax>3</EDISyntax>
              <CommunicationAddress Type="SMTP">ediel@7Hkraft.se</CommunicationAddress>
              <InterchangePartyId IdCodeQualifier="ZZ">60900</InterchangePartyId>
              <PartyId IdCodeQualifier="160" IdCodeResponsible="SVK">60900</PartyId>
            </EDIFACTDetails>
            <EDIFACTDetails Type="UTILTS">
              <EDICharset>UNOC</EDICharset>
              <EDISyntax>3</EDISyntax>
              <CommunicationAddress Type="SMTP">ediel@7Hkraft.se</CommunicationAddress>
              <InterchangePartyId IdCodeQualifier="ZZ">60900</InterchangePartyId>
              <PartyId IdCodeQualifier="SVK" IdCodeResponsible="260">60900</PartyId>
            </EDIFACTDetails>
          </EDIFACTAddressing>
          <NBSAddressing>
            <NBSDetails Type="NBS">
              <EnergyPartyId IdentifierType="EdielId">60900</EnergyPartyId>
            </NBSDetails>
          </NBSAddressing>
        </EdielAddressing>
      </Company>
    </Market>
  </CompanyListMessage>
</ExportPartyAddressingResponse>

我尝试了以下 SQL 语句,只是为了首先从 header 标签获取信息...

WITH XMLNAMESPACES(DEFAULT N'http://www.ediel.no/Export')
    SELECT
        t.file_name, t.file_created_time,
        h.value(N'(GeneratedAt/text())[1]', 'varchar(50)') h1,
        h.value(N'@GeneratedAt', 'varchar(50)') h2
    FROM
        load.ediel_actors t
    OUTER APPLY
        t.xml_data.nodes('/ExportPartyAddressingResponse/CompanyListMessage') AS header(h)

我不明白为什么我的 SQL 不工作。是命名空间定义还是取值代码?

我正在使用 SQL Server 2019。

两个问题:

(1):您的 XPath 表达式错误 - 您还必须包含 /Header 部分才能获得 <Header> XML 元素已选择

(2):您的 h1 表达式是错误的 - 因为您正在尝试获取 属性的值 在 XML 元素上,您需要在第二行中使用表达式。

所以试试这个:

WITH XMLNAMESPACES(DEFAULT N'http://www.ediel.no/Export')
    SELECT
        t.Col1,
        h.value(N'@GeneratedAt', 'varchar(50)') HeaderGeneratedAt,
        h.value(N'@SenderApplication', 'varchar(50)') SenderApplication
    FROM
        @datatbl t
    OUTER APPLY
        t.xmldata.nodes('/ExportPartyAddressingResponse/CompanyListMessage/Header') AS header(h)