【问题标题】:Xpath works for MSXML2.DOMDocument, but not for MSXML2.DOMDocument.6.0Xpath 适用于 MSXML2.DOMDocument,但不适用于 MSXML2.DOMDocument.6.0
【发布时间】:2020-09-24 10:18:44
【问题描述】:

为了在 MS Excel VBA 中解析我的 XML 文档,我必须使用 MSXML2.DOMDocument.6.0。

XPath 语句如下:

Public xml_document As Object
Public xml_namespace_uri As String
...
    xml_namespace_uri = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"

    Set xml_document = CreateObject("MSXML2.DOMDocument")

    xml_document.async = False
    xml_document.validateOnParse = True
    xml_document.LoadXML _
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
        "<Document xmlns=""" & xml_namespace_uri & """ " & _
        "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""/>"

    xml_document.SelectSingleNode("/Document").appendChild _
        xml_document.createNode(1, "CstmrDrctDbtInitn", xml_namespace_uri)

工作正常,但一旦我更换

    Set xml_document = CreateObject("MSXML2.DOMDocument")

通过

    Set xml_document = CreateObject("MSXML2.DOMDocument.6.0")

XPath 语句失败并退出包含子。谁能解释我在这里做错了什么?

2020-09-28 12:00:00

阅读所有建议和评论后,我将迈克尔的工作示例扩展如下:

Sub XmlText()

    Dim xml_namespace_uri As String
    Dim xml_document As Object
    Dim docnode01 As Object
    Dim docnode02 As Object

    xml_namespace_uri = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"
    
    Set xml_document = CreateObject("MSXML2.DOMDocument.6.0")
    xml_document.setProperty "SelectionNamespaces", "xmlns:doc='" & xml_namespace_uri & "'"
    
    xml_document.async = False
    xml_document.validateOnParse = True
    xml_document.LoadXML _
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
        "<Document xmlns:doc=""" & xml_namespace_uri & """ " & _
        "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""/>"

    Set docnode01 = xml_document.SelectSingleNode("/Document")
    docnode01.appendChild _
        xml_document.createNode(1, "CstmrDrctDbtInitn", "xmlns:doc='" & xml_namespace_uri & "'")

    Set docnode02 = xml_document.SelectSingleNode("/Document/CstmrDrctDbtInitn")
    docnode02.appendChild _
        xml_document.createNode(1, "GrpHdr", "xmlns:doc='" & xml_namespace_uri & "'")

    Debug.Print xml_document.XML

End Sub

现在,程序在第二个 appendChild 语句处得到一个小插曲,并产生错误消息:“执行时出现错误 91:尚未设置对象变量或块变量”(免费翻译自荷兰语)。

这可能是“Dim As Object”语句,不够具体吗?我尝试了其他几种数据类型,都产生了一些错误消息。

2020-09-28 12:10:00

顺便说一句,添加以下语句也不能解决这个问题:

    xml_document.setProperty "SelectionLanguage", "XPath"

2020-09-28 13:05

在回答 Parfait 的问题时,我尝试遵循“https://stackoverflow.com/questions/58026296/why-does-parsing-xml-document-using-msxml-v3-0-工作但 msxml-v6-0-doesnt”。我在 XPath 表达式中添加了命名空间前缀,如下所示:

    Set docnode01 = xml_document.SelectSingleNode("/doc:Document")

但随后下一个 appendChild 语句失败:“未设置对象变量”。抱歉,我可能没有完全理解我在做什么,以下尝试也失败了:

    Set docnode01 = xml_document.SelectSingleNode("/xmlns:doc:Document")

【问题讨论】:

  • 不要告诉我们失败了——告诉我们它是如何失败的。所有诊断都始于对症状的描述。它看起来像一个命名空间问题,但我对 MSXML 的了解还不够,无法比这更具体。
  • 您可能有兴趣在XML parse/VBA Excel - added hints@MichaelKay 上为版本控制历史添加提示
  • 它仍然不适合您吗?我已经在 MSO 2016 中测试了这个端到端。
  • 感谢 Michael,感谢 Parfait,感谢您的解决方案以及您让我跑步的尝试。唉!还没有动静。我让迈克尔的解决方案运行起来。但是,此解决方案没有通过根据“pain.008.001.02.xsd”解析它的审查:“CstmrDrctDbtInitn”元素中不允许命名空间属性。在这一点上,我真的很困惑!不是解决问题的正确心态。我当然希望我没有要求太多,如果我要求你再看看,尤其是我在帖子中添加的最后一句话。

标签: excel xml vba xpath msxml2


【解决方案1】:

正如@MichaelKay 所暗示的,问题似乎确实是 MSXML 版本之间的命名空间处理。

使用前缀设置SelectionNamespaces xml_doc.property 对我有用:xmlns:doc

Sub XmlText()
    xml_namespace_uri = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"
    
    Set xml_document = CreateObject("MSXML2.DOMDocument.6.0")
    xml_document.SetProperty "SelectionNamespaces", "xmlns:doc='" & xml_namespace_uri & "'"
    
    xml_document.async = False
    xml_document.validateOnParse = True
    xml_document.LoadXML _
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
        "<Document xmlns:doc=""" & xml_namespace_uri & """ " & _
        "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""/>"
    
    Set docNode = xml_document.SelectSingleNode("/Document")
    docNode.appendChild _
        xml_document.createNode(1, "CstmrDrctDbtInitn", "xmlns:doc='" & xml_namespace_uri & "'")
    MsgBox (docNode.XML)
End Sub

在消息框中显示:

<Document xmlns:doc="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  <CstmrDrctDbtInitn xmlns="xmlns:doc='urn:iso:std:iso:20022:tech:xsd:pain.008.001.02'"/>
</Document>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 2020-04-06
  • 2017-04-22
相关资源
最近更新 更多