0

Some files I need to process have this (called the "haves"):

<ApolloDataSet xmlns="http://irisoft.com/ApolloDataSet1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Other files in the same group have this (called the "have-nots"):

<ApolloDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  

I can set the default namespace for xpath with the below:

.setProperty "SelectionNamespaces", "xmlns:a='http://irisoft.com/ApolloDataSet1.xsd'"

That works for the haves, like a:/Element, but not the have-nots, since the xpath doesn't have the a: alias.

I tried removing the xmlns attribute before processing, in the hopes that I could just use an unaliased path for both, like /Element, but that only worked for the have-nots (the haves just returned nothing).

So is there a way to process both using the same alias, or no alias? I'm trying to either use the same alias for every file, regardless of "xmlns" being listed, or use no alias for either.

4

1 回答 1

0

好吧,我放弃了使用我试图避免的解决方法。但在我找到更好的选择之前,这里有一个适合这种情况的选择。(忘了提 - 为此使用 VBA。)

加载domDocument后,我使用getAttribute方法检查“xmlns”属性值是否为null。如果是,我通过vbnullstring 形成没有别名的xpath 字符串,这意味着xpath 查询适用于没有文件(即那些没有列出“xmlns”的文件)。如果它不为空,我将使用“xmlns”值为设置“SelectionNamespaces”辅助属性的 setProperty 方法构建一个字符串。

If IsNull(xPOG.DocumentElement.getAttribute("xmlns")) Then
    strAlias = vbNullString
Else
    strAlias = "a:"
    xPOG.setProperty "SelectionNamespaces", _
    "xmlns:" & Mid(strAlias, 1, 1) & "='" & xPOG.DocumentElement.getAttribute("xmlns") & "'"
End If

由此产生的处理呈现出一种我希望避免的相当丑陋的字符串构建形式。它使用带有 strAlias 变量的 xPath 查询将 IXMLDOMNode 设置为从 selectnodes 方法返回的节点。

Set xProducts = xPOG.SelectNodes("/" & strAlias & "ApolloDataSet/" _
                                & strAlias & "Products/" _
                                & strAlias & "Product/" _
                                & strAlias & "PR_UPC")

并不是说它不好,它只是似乎缺乏优雅并提醒我有时我知道的很少。这让我相信我错过了一些可以纠正这个问题的概念。但它有效,所以如果我找到更好的,我会跟进。如果对任何人有帮助,我确实找到了一篇涉及一些概念的知识库文章。不过,我使用的是 6.0,所以概念解释很好,只是不完全是我需要的。

PRB:MSXML 4.0 将子节点的 XML 命名空间属性设置为空值

于 2014-12-14T21:57:11.643 回答