将具有多个名称空间的XML解析为SQL的问题

时间:2018-10-15 13:00:41

标签: sql xml openxml

在使用双名称空间将XML中的信息解析为SQL时遇到问题。 看一下这段代码:

DECLARE @Handle AS INT; -- The handle of the XML data, passed to sp_xml_preparedocument
DECLARE @Xml AS NVARCHAR(1000); -- The XML document for this example


SET @Xml = N'
<SiBikNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.ws.bik.pl/ws/ki/2v2/types">
 <BIK_REQUEST>
    <siBikNetResponse>
        <consentDate>2018-07-29</consentDate>
        <citizenshipStatus>citizen</citizenshipStatus>
        <nationality>PL</nationality>
        <pesel>123</pesel>
    </siBikNetResponse>
 </BIK_REQUEST>
</SiBikNet>';


EXEC sys.sp_xml_preparedocument @Handle OUTPUT , @Xml, N'<SiBikNet xmlns:t="https://www.ws.bik.pl/ws/ki/2v2/types"/>'; --Prepare a parsed document 

SELECT *
FROM
       OPENXML(@Handle,'/t:SiBikNet/t:BIK_REQUEST/t:siBikNetResponse', 2)
           WITH (   nationality NVARCHAR(10) 't:nationality',
                    pesel  NVARCHAR(10)      't:pesel '
                );

EXEC sys.sp_xml_removedocument @Handle;  

哪个以2列的表格形式给我适当的输出。 但是,当我添加带有双重名称空间的一行时:那么我无法解析此信息:

DECLARE @Handle AS INT; -- The handle of the XML data, passed to sp_xml_preparedocument
DECLARE @Xml AS NVARCHAR(1000); -- The XML document for this example


SET @Xml = N'
<SiBikNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.ws.bik.pl/ws/ki/2v2/types">
 <BIK_REQUEST xmlns="">
    <siBikNetResponse>
        <consentDate>2018-07-29</consentDate>
        <citizenshipStatus>citizen</citizenshipStatus>
        <nationality>PL</nationality>
        <pesel>123</pesel>
    </siBikNetResponse>
 </BIK_REQUEST>
</SiBikNet>';


EXEC sys.sp_xml_preparedocument @Handle OUTPUT , @Xml, N'<SiBikNet xmlns:t="https://www.ws.bik.pl/ws/ki/2v2/types"/>'; --Prepare a parsed document 

SELECT *
FROM
       OPENXML(@Handle,'/t:SiBikNet/t:BIK_REQUEST/t:siBikNetResponse', 2)
           WITH (   nationality NVARCHAR(10) 't:nationality',
                    pesel  NVARCHAR(10)      't:pesel '
                );

EXEC sys.sp_xml_removedocument @Handle; 

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

当我陷入匿名名称空间或奇数名称空间组合的困扰时,最简单的方法是只使用XPath函数local-name()。像这样:

DECLARE @Handle AS INT; -- The handle of the XML data, passed to sp_xml_preparedocument
DECLARE @Xml AS NVARCHAR(1000); -- The XML document for this example


SET @Xml = N'
<SiBikNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.ws.bik.pl/ws/ki/2v2/types">
 <BIK_REQUEST xmlns="">
    <siBikNetResponse>
        <consentDate>2018-07-29</consentDate>
        <citizenshipStatus>citizen</citizenshipStatus>
        <nationality>PL</nationality>
        <pesel>98070902702</pesel>
    </siBikNetResponse>
 </BIK_REQUEST>
</SiBikNet>';


EXEC sys.sp_xml_preparedocument @Handle OUTPUT , @Xml, N'<SiBikNet xmlns:t="https://www.ws.bik.pl/ws/ki/2v2/types"/>'; --Prepare a parsed document 

SELECT *
FROM
       OPENXML(@Handle,'/*[local-name()="SiBikNet"]/*[local-name()="BIK_REQUEST"]/*[local-name()="siBikNetResponse"]', 2)
           WITH (   nationality NVARCHAR(10) ,--'t:nationality',
                    pesel  NVARCHAR(10)      --'t:pesel '
                );

EXEC sys.sp_xml_removedocument @Handle;