简体   繁体   English

Xquery TSQL:遍历具有多个xmlns,xmlns:xsd和xmlns:xsi的父节点时如何检索元素数据

[英]Xquery TSQL: How to retrieve element data when traversing parents nodes with multiple xmlns, xmlns:xsd and xmlns:xsi

I need to retrieve the 'true' value in processquotedetail element. 我需要在processquotedetail元素中检索“ true”值。

What xmlnamespaces would i have to provide, and specify in an outer apply in order to successfully retrieve 'true' 我必须提供什么xmlnamespace,并在外部应用中指定以便成功检索“ true”

I have changed the xmlns: values 我已经更改了xmlns:值

I have tried the below and several other slight alternatives, all of which are returning null. 我已经尝试了以下方法以及其他一些替代方法,所有方法都返回null。 Can you explain why the below doesn't work, and why what does work, does? 您能否解释以下内容为何无效以及为何有效?

Thank you 谢谢

The query: 查询:

WITH XMLNAMESPACES('web.servivces.com"' as  pmq,
                    'www.test.com/' as q,
                    'www.test.com/request/' as r )

select 
x.RequestID,       dc.tx.value('pmq:Control[1]/pmq:ProcessQuoteDetail[1]','nvarchar(max)')
dc.tx.value('Control[1]/ProcessQuoteDetail[1]','nvarchar(max)')

from [dbo].[uvw_decompressXML_DC_Quote] x  

outer apply x.DCRequest.nodes('server/requests/Session.setDocumentRq/session/data/policy
r:AdditionalData/r:KeyValuePair/q:Key/q:Value/pmq:PMQuoteData') as dc(tx) 

The XML: XML:

XML: 
<server>
  <requests>
    <Session.loginRq userName="admin" password="admin" />
    <Session.setDocumentRq>
      <session>
        <data>
          <MotorQuoteID />
          <TransactionReason>Rating</TransactionReason>
          <policy>
            <AdditionalData xmlns="www.test.com/request/" 
             xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
              <KeyValuePair>
                <Key xmlns="www.test.com/">AggregatorXML</Key>
                <Value xmlns="www.test.com/">
                  <PMQuoteData xmlns="web.servivces.com">
                    <Control>
                      <ProcessQuoteDetail>true</ProcessQuoteDetail>

SQL 的SQL

-- DDL and data population, start
DECLARE @tbl TABLE (ID INT IDENTITY(1,1) PRIMARY KEY, xmlData XML NOT NULL);
INSERT INTO @tbl
VALUES
('<server>
    <requests>
        <Session.loginRq userName="admin" password="admin"/>
        <Session.setDocumentRq>
            <session>
                <data>
                    <MotorQuoteID/>
                    <TransactionReason>Rating</TransactionReason>
                    <policy>
                        <AdditionalData xmlns="www.test.com/request/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                            <KeyValuePair>
                                <Key xmlns="www.test.com/">AggregatorXML</Key>
                                <Value xmlns="www.test.com/">
                                    <PMQuoteData xmlns="web.servivces.com">
                                        <Control>
                                            <ProcessQuoteDetail>true</ProcessQuoteDetail>
                                        </Control>
                                    </PMQuoteData>
                                </Value>
                            </KeyValuePair>
                        </AdditionalData>
                    </policy>
                </data>
            </session>
        </Session.setDocumentRq>
    </requests>
</server>');
-- DDL and data population, end

WITH XMLNAMESPACES('web.servivces.com' as ns3,
                    'www.test.com/' as ns2,
                    'www.test.com/request/' as ns1 )
SELECT tbl.id, col.value('(ns3:ProcessQuoteDetail)[1]', 'VARCHAR(10)') as [ProcessQuoteDetail]
FROM @tbl AS tbl
CROSS APPLY tbl.XMLDATA.nodes('/server/requests/Session.setDocumentRq/session/data/policy/ns1:AdditionalData/ns1:KeyValuePair/ns2:Value/ns3:PMQuoteData/ns3:Control') tab(col);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM