如何从SQL XML查询中选择标记?

时间:2021-06-15 22:50:29

How can I retrieve the fields within an XML field in MS SQL?

如何在MS SQL中检索XML字段中的字段?

Every query I try does not work as intended whenever I use this XML code:

每当我使用此XML代码时,我尝试的每个查询都无法正常工作:

<soap:Envelope xmlns:xsi="[URI]" xmlns:xsd="[URI]" xmlns:soap="[URI]">
  <soap:Body>
    <RunPackage xmlns="[URI]">
      <xmlDoc>
        <Request>
          <SubscriberCode>76547654</SubscriberCode>
          <CompanyCode></CompanyCode>
        </Request>
      </xmlDoc>
    </RunPackage>
  </soap:Body>
</soap:Envelope>

I don't know how to reference the first two tags. I've tried

我不知道如何引用前两个标签。我试过了

SELECT TransactionID, T2.Loc.query('data(Request/SubscriberCode)') as 'SubscriberCode'
FROM   TempWorksRequest
CROSS APPLY RequestXML.nodes('soap:Envelope/soap:Body/RunPackage/xmlDoc') as T2(Loc) 

With no luck.

没有运气。

1 个解决方案

#1


You need to declare the XML namespaces ("soap" in this case, plus another one for the node and anything below) in your XQuery operations:

您需要在XQuery操作中声明XML命名空间(在这种情况下为“soap”,另外还有一个用于节点和下面的任何内容):

SELECT 
   TransactionID, 
   T2.Loc.query('declare namespace ns="[URI1]";data(ns:Request/ns:SubscriberCode)') 
     as 'SubscriberCode'
FROM   
   TempWorksRequest
CROSS APPLY 
   RequestXML.nodes('declare namespace soap="[URI]";
                     declare namespace ns="[URI1]";
                     soap:Envelope/soap:Body/ns:RunPackage/ns:xmlDoc') as T2(Loc)

[URI1] needs to be the URI that's defined on the <RunPackage> tag.

[URI1]需要是 标记上定义的URI。

Marc

#1


You need to declare the XML namespaces ("soap" in this case, plus another one for the node and anything below) in your XQuery operations:

您需要在XQuery操作中声明XML命名空间(在这种情况下为“soap”,另外还有一个用于节点和下面的任何内容):

SELECT 
   TransactionID, 
   T2.Loc.query('declare namespace ns="[URI1]";data(ns:Request/ns:SubscriberCode)') 
     as 'SubscriberCode'
FROM   
   TempWorksRequest
CROSS APPLY 
   RequestXML.nodes('declare namespace soap="[URI]";
                     declare namespace ns="[URI1]";
                     soap:Envelope/soap:Body/ns:RunPackage/ns:xmlDoc') as T2(Loc)

[URI1] needs to be the URI that's defined on the <RunPackage> tag.

[URI1]需要是 标记上定义的URI。

Marc