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

时间:2016-07-05 11:02:24

标签: sql xml

我有一个带命名空间的XML。我无法使用sp_xml_preparedocument解析它。如果从XML中删除名称空间,我能够解析。我还尝试将名称空间传递给Sp作为参数。但它没有返回任何结果。 下面是命名空间节点。

'<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"
/>'

先谢谢。

1 个答案:

答案 0 :(得分:0)

使用sp_xml_preparedocument读取XML已过时,应替换为相应的XPath方法!

从我的问题我假设,真正的XML更大。这只会检索releaseID以显示操作方法 ...

试试这样:

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

没有命名空间的另一种方法是使用通配符:

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

更新

如果你必须坚持使用过时的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