使用 SQL 在 XML 中导入字段无效

Import field in XML using SQL not working

我一直在尝试在我的示例中导入字段 GivenName XML 但由于某种原因它不起作用,我一直在使用以下 SQL 查询,我想我是使用正确的字段和正确的节点,但我不能 100% 确定 XMLNameSpaces

非常感谢您的帮助

这是示例 SQL 我正在使用的查询:

DECLARE @xml XML = (SELECT [Xml] FROM ExampleTable)

 ;WITH XMLNAMESPACES (DEFAULT 'http://www.opentravel.org/OTA/2003/05','http://www.w3.org/2003/05/soap-envelope' )

select FirstName = ProfileInfo.value('Profiles[1]/ProfileInfo[1]/Profile[1]/Customer[1]/PersonName[1]/@GivenName', 'nvarchar(255)')
    FROM @xml.nodes('Envelope/Body/OTA_Example/Info/Infos/ResUser') as T1(Profiles) 
      outer apply T1.Profiles.nodes('ResUser2') as T2(ProfileInfo)
          

这是我用于导入的示例 XML:

<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope">
  <soap2:Header xmlns:htng="http://htng.org/1.3/Header/" xmlns:wsa="http://www.w3.org/2005/08/addressing" 
  xmlns:wss="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:htnga="http://htng.org/PWSWG/2007/02/AsyncHeaders" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:soap2="http://www.w3.org/2003/05/soap-envelope">
    <wsa:Action>Example</wsa:Action>
    <wsa:ReplyTo>
      <wsa:Address>Example2</wsa:Address>
    </wsa:ReplyTo>
    <htnga:ReplyTo>
      <wsa:Address>Example3</wsa:Address>
    </htnga:ReplyTo>
    <wsa:MessageID>123</wsa:MessageID>
    </soap2:Header>
  <Body>
    <OTA_Example xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.opentravel.org/OTA/2003/05" Version="5.000" >
      <Info>
        <Infos CreateDateTime="2021-09-20T06:52:40" CreatorID="User">
          <UniqueID Type="1" ID="12341251" />
           <ResUser>
            <ResUser2 ResGuestRPH="1" PrimaryIndicator="true">
              <Profiles>
                <ProfileInfo>
                    <Profile ProfileType="1">
                    <Customer>
                      <PersonName>
                        <NamePrefix>Mr.</NamePrefix>
                        <GivenName>FirstnameTest</GivenName>
                        <Surname>LastnameTest</Surname>
                      </PersonName>
                      </Customer>
                  </Profile>
                </ProfileInfo>
              </Profiles>
            </ResUser2>
          </ResUser>
         </Infos>
      </Info>
    </OTA_Example>
  </Body>
</Envelope>

GivenName 不是一个属性,所以你不应该为它使用 @

不清楚您为什么需要 .nodes,仅当有多个节点需要分成单独的行时才需要

你也可以select直接从ExampleTable出来,你不需要把它存储在一个变量中。

;WITH XMLNAMESPACES (
   'http://www.w3.org/2003/05/soap-envelope' AS soap,
   DEFAULT 'http://www.opentravel.org/OTA/2003/05')

select FirstName = [XML].value('(soap:Envelope/soap:Body/OTA_Example/Info/Infos/ResUser/ResUser2/Profiles/ProfileInfo/Profile/Customer/PersonName/GivenName/text())[1]', 'nvarchar(255)')
    FROM ExampleTable

db<>fiddle