原因
这个 XML 是如何生成的?你遇到的问题,在这里:
<verblijfsadres xmlns="http://www.egem.nl/StUF/sector/bg/0310">
<gor.openbareRuimteNaam>Westmalledreef 45</gor.openbareRuimteNaam>
<wpl.woonplaatsNaam>B-16753</wpl.woonplaatsNaam>
</verblijfsadres>
<voornamen xmlns="http://www.egem.nl/StUF/sector/bg/0310">Erik</voornamen>
您正在声明新的默认命名空间,其 URL 与其他命名空间的 URL 相同。
在下面的代码中,我删除了这些额外的命名空间,并使用您的代码稍作改动:voornamen 和 gor.openbareRuimteNaam 没有前缀。这有效:
DECLARE @xml xml, @hdoc int
SET @xml = '
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.egem.nl/StUF/StUF0301" xmlns:ns2="http://www.egem.nl/StUF/sector/zkn/0310" xmlns:bg="http://www.egem.nl/StUF/sector/bg/0310">
<SOAP-ENV:Body>
<ns2:zakLk01>
<ns2:object ns1:entiteittype="ZAK" ns1:verwerkingssoort="T">
<ns2:heeftBetrekkingOp ns1:entiteittype="ZAKOBJ" ns1:verwerkingssoort="T">
<ns2:gerelateerde>
<ns2:natuurlijkPersoon ns1:entiteittype="NPS" ns1:verwerkingssoort="I">
<verblijfsadres>
<gor.openbareRuimteNaam>Westmalledreef 45</gor.openbareRuimteNaam>
<wpl.woonplaatsNaam>B-16753</wpl.woonplaatsNaam>
</verblijfsadres>
<voornamen>Erik</voornamen>
</ns2:natuurlijkPersoon>
</ns2:gerelateerde>
</ns2:heeftBetrekkingOp>
</ns2:object>
</ns2:zakLk01>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'
EXEC sp_xml_preparedocument
@hDoc OUTPUT,
@XML,
'<root xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://www.egem.nl/StUF/StUF0301"
xmlns:ns2="http://www.egem.nl/StUF/sector/zkn/0310"
xmlns:bg="http://www.egem.nl/StUF/sector/bg/0310>"/>'
SELECT * FROM OPENXML(@hdoc, '/SOAP-ENV:Envelope') --Row Pattern
WITH
(
Voornamen VARCHAR(50) './/ns2:heeftBetrekkingOp/ns2:gerelateerde/ns2:natuurlijkPersoon/voornamen',
Adres VARCHAR(100) './/ns2:heeftBetrekkingOp/ns2:gerelateerde/ns2:natuurlijkPersoon/verblijfsadres/gor.openbareRuimteNaam'
)
EXEC sp_xml_removedocument @hdoc --Releasing memory
分析您的 XML
像这样尝试不使用WITH 的原始查询
SELECT * FROM OPENXML(@hdoc, '/SOAP-ENV:Envelope')
你会得到一个列表,XML 引擎如何看到你的元素(片段):
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| natuurlijkPersoon | ns2 | http://www.egem.nl/StUF/sector/zkn/0310 | NULL | NULL | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| entiteittype | ns1 | http://www.egem.nl/StUF/StUF0301 | NULL | NULL | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| #text | NULL | NULL | NULL | NULL | NPS |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| verwerkingssoort | ns1 | http://www.egem.nl/StUF/StUF0301 | NULL | NULL | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| #text | NULL | NULL | NULL | NULL | I |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| verblijfsadres | NULL | http://www.egem.nl/StUF/sector/bg/0310 | NULL | NULL | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| xmlns | xmlns | NULL | NULL | NULL | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| #text | NULL | NULL | NULL | NULL | http://www.egem.nl/StUF/sector/bg/0310 |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| gor.openbareRuimteNaam | NULL | http://www.egem.nl/StUF/sector/bg/0310 | NULL | NULL | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| #text | NULL | NULL | NULL | NULL | Westmalledreef 45 |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| wpl.woonplaatsNaam | NULL | http://www.egem.nl/StUF/sector/bg/0310 | NULL | 20 | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| #text | NULL | NULL | NULL | NULL | B-16753 |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
| voornamen | NULL | http://www.egem.nl/StUF/sector/bg/0310 | NULL | 18 | NULL |
+------------------------+-------+-----------------------------------------+------+------+----------------------------------------+
您可以看到,您要查找的元素位于命名空间中,但没有前缀...
所有这些只是您的查询不起作用的一些解释。但是:
您应该使用不同的方法!
FROM OPENXML 与相应的 SP 准备和删除文档已过时,不应再使用(存在罕见的例外情况)。而是使用适当的methods the XML data type provides。
尽可能明确,最好是这样:
;WITH XMLNAMESPACES (
'http://schemas.xmlsoap.org/soap/envelope/' AS [SOAP-ENV],
'http://www.egem.nl/StUF/StUF0301' AS ns1,
'http://www.egem.nl/StUF/sector/zkn/0310' AS ns2,
'http://www.egem.nl/StUF/sector/bg/0310' AS bg
)
SELECT @xml.value('(SOAP-ENV:Envelope/SOAP-ENV:Body/ns2:zakLk01/ns2:object/ns2:heeftBetrekkingOp/ns2:gerelateerde/ns2:natuurlijkPersoon/bg:voornamen/text())[1]','VARCHAR(50)') AS Voornamen
,@xml.value('(SOAP-ENV:Envelope/SOAP-ENV:Body/ns2:zakLk01/ns2:object/ns2:heeftBetrekkingOp/ns2:gerelateerde/ns2:natuurlijkPersoon/bg:verblijfsadres/bg:gor.openbareRuimteNaam/text())[1]','VARCHAR(100)') AS Adres
简单又丑陋的就是这个
SELECT @xml.value('(//*:natuurlijkPersoon/*:voornamen/text())[1]','VARCHAR(50)') AS Voornamen
,@xml.value('(//*:natuurlijkPersoon/*:verblijfsadres/*:gor.openbareRuimteNaam/text())[1]','VARCHAR(100)') AS Adres