oracle Plsql XML解析-命名空间问题再次出现:-(

arknldoa  于 2023-04-29  发布在  Oracle
关注(0)|答案(1)|浏览(134)

我的选择有问题。我想要获取的值的标记有一个命名空间,稍后,命名空间可以不同,所以我不能硬编码它。
到目前为止,'//*[local-name()='语法是有效的,除非标签有一个我想从中获取值的名称空间。
现在返回的值为null。

select 
test.test_customer_id,
test.test_customer_id_2
 
from (
       select column_value
       from XMLTABLE(q'~//*[local-name()='ResponseData']~'
       PASSING
            (select xmltype('<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
                               <S:Body>
                                  <V2 xmlns:ns27="http://testnamespace">
                                     <ResponseData>
                                        <CustomerData>
                                           <ns27:TestCustomerId>27295343</ns27:TestCustomerId>
                                           <ns27:TestCustomerId2>27295343</ns27:TestCustomerId2>
                                        </CustomerData>
                                     </ResponseData>
                                  </V2>
                               </S:Body>
                            </S:Envelope>'
                            )
              from dual))) dlcp,
       XMLTABLE('//CustomerData' PASSING dlcp.column_value COLUMNS
                test_customer_id VARCHAR2(100) PATH 'TestCustomerId',
                test_customer_id_2 VARCHAR2(100) PATH 'TestCustomerId2') test;

我可以在不声明和硬编码ns 27命名空间的情况下修复这个问题吗?

eqzww0vc

eqzww0vc1#

如果你真的不知道命名空间,那么你可以用*来通配符命名空间:

-- wildcards
select 
test.test_customer_id,
test.test_customer_id_2
 
from (
       select column_value
       from XMLTABLE(q'~//*[local-name()='ResponseData']~'
       PASSING
            (select xmltype('<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
                               <S:Body>
                                  <V2 xmlns:ns27="http://testnamespace">
                                     <ResponseData>
                                        <CustomerData>
                                           <ns27:TestCustomerId>27295343</ns27:TestCustomerId>
                                           <ns27:TestCustomerId2>27295343</ns27:TestCustomerId2>
                                        </CustomerData>
                                     </ResponseData>
                                  </V2>
                               </S:Body>
                            </S:Envelope>'
                            )
              from dual))) dlcp,
       XMLTABLE('//*:CustomerData' PASSING dlcp.column_value COLUMNS
                test_customer_id VARCHAR2(100) PATH '*:TestCustomerId',
                test_customer_id_2 VARCHAR2(100) PATH '*:TestCustomerId2') test;
TEST_CUSTOMER_ID测试_CUSTOMER_ID_2
2729534327295343

您还可以简化为单个XMLTable调用:

select 
test.test_customer_id,
test.test_customer_id_2
from XMLTABLE(q'~//*[local-name()='ResponseData']/*:CustomerData~'
    PASSING
        (select xmltype('<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
                               <S:Body>
                                  <V2 xmlns:ns27="http://testnamespace">
                                     <ResponseData>
                                        <CustomerData>
                                           <ns27:TestCustomerId>27295343</ns27:TestCustomerId>
                                           <ns27:TestCustomerId2>27295343</ns27:TestCustomerId2>
                                        </CustomerData>
                                     </ResponseData>
                                  </V2>
                               </S:Body>
                            </S:Envelope>'
                            )
              from dual)
    COLUMNS
        test_customer_id VARCHAR2(100) PATH '*:TestCustomerId',
        test_customer_id_2 VARCHAR2(100) PATH '*:TestCustomerId2'
) test;
TEST_CUSTOMER_ID测试_CUSTOMER_ID_2
2729534327295343

或:

select 
test.test_customer_id,
test.test_customer_id_2
from (select xmltype('<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
                               <S:Body>
                                  <V2 xmlns:ns27="http://testnamespace">
                                     <ResponseData>
                                        <CustomerData>
                                           <ns27:TestCustomerId>27295343</ns27:TestCustomerId>
                                           <ns27:TestCustomerId2>27295343</ns27:TestCustomerId2>
                                        </CustomerData>
                                     </ResponseData>
                                  </V2>
                               </S:Body>
                            </S:Envelope>'
                            ) as xml
              from dual) dlcp
cross apply XMLTABLE(q'~//*[local-name()='ResponseData']/*:CustomerData~'
    PASSING dlcp.xml
    COLUMNS
        test_customer_id VARCHAR2(100) PATH '*:TestCustomerId',
        test_customer_id_2 VARCHAR2(100) PATH '*:TestCustomerId2'
) test;
TEST_CUSTOMER_ID测试_CUSTOMER_ID_2
2729534327295343

fiddle

相关问题