简体   繁体   English

存在命名空间时,无法使用Sp_XML_Prepare文档解析XML

[英]Unable to Parse XML using Sp_XML_Prepare document when namespaces are present

I have an XML with namespaces. 我有一个带有名称空间的XML。 I am unable to parse it using sp_xml_preparedocument. 我无法使用sp_xml_preparedocument对其进行解析。 I am able to parse if name spaces are removed from XML. 我能够解析是否从XML中删除了名称空间。 Also I tried passing name spaces to Sp as parameter. 我也尝试将名称空间传递给Sp作为参数。 But it is not returning any result. 但是它没有返回任何结果。 Below is the Namespaces node. 以下是“命名空间”节点。

'<SyncPurchaseOrder 
xmlns="http://schema.inf.com/infOAGIS/2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schema.inf.com/infOAGIS/2 http://schema.inf.com/2.10.0/infOAGIS/BODs/Developer/SyncPurchaseOrder.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
releaseID="9.2"
versionID="2.10.0"
/>'

Thanks in Advance. 提前致谢。

Reading an XML with sp_xml_preparedocument is out-dated and should be replaced by the appropriate XPath-methods! 使用sp_xml_preparedocument读取XML已过时,应由适当的XPath方法代替!

From your question I assume, that the real XML is bigger. 根据您的问题,我认为真正的XML更大。 This will only retrieve the releaseID to show the how-to-do ... 这只会检索releaseID以显示操作方法 ...

Try it like this: 像这样尝试:

DECLARE @x XML=
'<SyncPurchaseOrder 
xmlns="http://schema.inf.com/infOAGIS/2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schema.inf.com/infOAGIS/2 http://schema.inf.com/2.10.0/infOAGIS/BODs/Developer/SyncPurchaseOrder.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
releaseID="9.2"
versionID="2.10.0"
/>';

WITH XMLNAMESPACES(DEFAULT 'http://schema.inf.com/infOAGIS/2'
                  ,'http://www.w3.org/2001/XMLSchema-instance' AS xsi
                  ,'http://schema.inf.com/infOAGIS/2 http://schema.inf.com/2.10.0/infOAGIS/BODs/Developer/SyncPurchaseOrder.xsd' AS xsd)
SELECT @x.value('(/SyncPurchaseOrder/@releaseID)[1]','varchar(max)') AS releaseID

Another way to get away without the namespaces was a wildcard: 没有命名空间的另一种逃避方法是通配符:

SELECT @x.value('/*:SyncPurchaseOrder[1]/@releaseID','varchar(max)') AS releaseID

UPDATE 更新

If you have to stick with the out-dated FROM OPENXML you might do this 如果您必须坚持使用过时的FROM OPENXML ,则可以这样做

SELECT @XML = '<SyncPurchaseOrder 
xmlns="http://schema.inf.com/infOAGIS/2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schema.inf.com/infOAGIS/2 http://schema.inf.com/2.10.0/infOAGIS/BODs/Developer/SyncPurchaseOrder.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
releaseID="9.2"
versionID="2.10.0"
/>'

EXEC sp_xml_preparedocument @hDoc OUTPUT
                           ,@XML
                           ,'<SyncPurchaseOrder xmlns:d="http://schema.inf.com/infOAGIS/2"/>' --name it "d" as "default", but can be any name

SELECT releaseID
FROM OPENXML(@hDoc, 'd:SyncPurchaseOrder')
WITH 
(
releaseID   [nvarchar](20)  './@releaseID'
)

EXEC sp_xml_removedocument @hDoc
GO

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

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