How to use XQuery/T-SQL to parse customer xml that contains nested default namesapces

萝らか妹 提交于 2019-12-24 04:21:58

问题


I have a customer using a platform we have developed that allows customer webservice return data to be parsed by a stored procedure in MSSQL. We have a customer sending xml back that contains nested xmlns="xxxxxx" declarations that are to different URI locations. First off is this valid? And secondly is it possible to parse using XQuery? I have tried using both .value and .query capabilities of the t-sql xml datatype to try to work through the issue but the point where the "second default namespace" comes into play seems to be a boundary that is nearly impossible to cross using techniques I am aware of. Here is a sample xml response:

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
     <soapenv:Body>
      <FF_LOG_RSPD_CONT xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/X.V1">
       <FF_LOGIN_WSDL_RSPD>
        <FF_PYIVR_RS_DER xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/Y.V1">
         <EMPLID>111111</EMPLID>
         <PIN>2222</PIN>
         <NEW_PIN_FLAG>N</NEW_PIN_FLAG>
         <REVOKE_FLAG>N</REVOKE_FLAG>
         <LAST_4_OF_SSN></LAST_4_OF_SSN>
         <WSDL_SUCCESS_FLAG>Y</WSDL_SUCCESS_FLAG>
        </FF_PYIVR_RS_DER>
       </FF_LOGIN_WSDL_RSPD>
      </FF_LOG_RSPD_CONT>
     </soapenv:Body>
    </soapenv:Envelope>

回答1:


Can you try explicitly calling out the namespaces?

declare @theXml XML = '<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
     <soapenv:Body>
      <FF_LOG_RSPD_CONT xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/X.V1">
       <FF_LOGIN_WSDL_RSPD>
        <FF_PYIVR_RS_DER xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/Y.V1">
         <EMPLID>111111</EMPLID>
         <PIN>2222</PIN>
         <NEW_PIN_FLAG>N</NEW_PIN_FLAG>
         <REVOKE_FLAG>N</REVOKE_FLAG>
         <LAST_4_OF_SSN></LAST_4_OF_SSN>
         <WSDL_SUCCESS_FLAG>Y</WSDL_SUCCESS_FLAG>
        </FF_PYIVR_RS_DER>
       </FF_LOGIN_WSDL_RSPD>
      </FF_LOG_RSPD_CONT>
     </soapenv:Body>
    </soapenv:Envelope>'

SELECT
    @theXml.value('
        declare namespace ns1="http://schemas.xmlsoap.org/soap/envelope/";
        declare namespace ns2="http://xmlns.oracle.com/Enterprise/Tools/schemas/X.V1";
        declare namespace ns3="http://xmlns.oracle.com/Enterprise/Tools/schemas/Y.V1";
        (/ns1:Envelope/ns1:Body/ns2:FF_LOG_RSPD_CONT/ns2:FF_LOGIN_WSDL_RSPD/ns3:FF_PYIVR_RS_DER/ns3:EMPLID)[1]', 
        'nvarchar(max)') as result

Results:

111111


来源:https://stackoverflow.com/questions/20056208/how-to-use-xquery-t-sql-to-parse-customer-xml-that-contains-nested-default-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!