VBA XML 选择命名空间问题
VBA XML Selection Namespace issue
所以我确定这是可以解决的,但我通常不必在 VBA XML 代码中处理 XML 名称空间。所以,我们有一个文件,它实际上是一个名为 Flag_of_the_United_Kingdom.svg
的 SVG 文件,这是文件内容
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 30" width="1200" height="600">
<clipPath id="t">
<path d="M30,15 h30 v15 z v15 h-30 z h-30 v-15 z v-15 h30 z"/>
</clipPath>
<path d="M0,0 v30 h60 v-30 z" fill="#00247d"/>
<path d="M0,0 L60,30 M60,0 L0,30" stroke="#fff" stroke-width="6"/>
<path d="M0,0 L60,30 M60,0 L0,30" clip-path="url(#t)" stroke="#cf142b" stroke-width="4"/>
<path d="M30,0 v30 M0,15 h60" stroke="#fff" stroke-width="10"/>
<path d="M30,0 v30 M0,15 h60" stroke="#cf142b" stroke-width="6"/>
</svg>
这只是一个示例 SVG 文件,但它适用于其他 SVG 文件,要使 Internet Explorer 呈现该文件,它需要根元素上的名称空间属性。
但是,我想在 VBA 中打开一个 SVG 文件并开始操作它,但我的代码在命名空间问题上出错了。这是我的代码,它位于同一目录中工作簿的标准模块中...
Option Explicit
Sub LoadSVGFile()
'* Tools->References->Microsoft Xml, v6.0
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Dim sSVGPath As String
sSVGPath = fso.BuildPath(ThisWorkbook.Path, "Flag_of_the_United_Kingdom.svg")
If fso.FileExists(sSVGPath) Then
Dim dom As MSXML2.DOMDocument60
Set dom = New MSXML2.DOMDocument60
dom.Load sSVGPath
Debug.Assert dom.parseError = 0
'xmlns="http://www.w3.org/2000/svg
'dom.namespaces.Add "http://www.w3.org/2000/svg", "xmlns"
dom.setProperty "SelectionNamespaces", "xmlns=""http://www.w3.org/2000/svg"""
'Call dom.setProperty("SelectionLanguage", "XPath")
Dim xmlSVG As MSXML2.IXMLDOMElement
Set xmlSVG = dom.SelectSingleNode("svg")
Debug.Assert Not dom.SelectSingleNode("svg") Is Nothing
Debug.Assert Not dom.SelectSingleNode("/svg") Is Nothing
If xmlSVG Is Nothing Then End
'* rest of code follows here but is not shown
Debug.Print "'* rest of code follows here but is not shown"
End If
End Sub
当我从根元素中删除命名空间属性时,上面的代码可以工作,但这并不好,因为这些文件是由 Inkscape 等生成的。
我确定这是一个 SelectionNamespaces
属性 问题,但我无法让它工作,我相信有人可以轻松解决这个问题。
更新:谷歌搜索看起来这是预期的行为 https://support.microsoft.com/en-gb/kb/288147
据我所知,selectionNamespace 是用于选择语言 (xPath) 的 namspaces/prefixes 列表。它用于在 xPath 中的名称空间之间切换。像这样尝试:
dom.setProperty "SelectionNamespaces", "xmlns:svg=""http://www.w3.org/2000/svg"""
Dim xmlSVG As MSXML2.IXMLDOMElement
Set xmlSVG = dom.SelectSingleNode("svg:svg")
Debug.Assert Not dom.SelectSingleNode("svg:svg") Is Nothing
Debug.Assert Not dom.SelectSingleNode("/svg:svg") Is Nothing
P.S.: 自从我上次使用 Windows 以来已经有一段时间了,所以我不能保证任何东西:-(
所以我确定这是可以解决的,但我通常不必在 VBA XML 代码中处理 XML 名称空间。所以,我们有一个文件,它实际上是一个名为 Flag_of_the_United_Kingdom.svg
的 SVG 文件,这是文件内容
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 30" width="1200" height="600">
<clipPath id="t">
<path d="M30,15 h30 v15 z v15 h-30 z h-30 v-15 z v-15 h30 z"/>
</clipPath>
<path d="M0,0 v30 h60 v-30 z" fill="#00247d"/>
<path d="M0,0 L60,30 M60,0 L0,30" stroke="#fff" stroke-width="6"/>
<path d="M0,0 L60,30 M60,0 L0,30" clip-path="url(#t)" stroke="#cf142b" stroke-width="4"/>
<path d="M30,0 v30 M0,15 h60" stroke="#fff" stroke-width="10"/>
<path d="M30,0 v30 M0,15 h60" stroke="#cf142b" stroke-width="6"/>
</svg>
这只是一个示例 SVG 文件,但它适用于其他 SVG 文件,要使 Internet Explorer 呈现该文件,它需要根元素上的名称空间属性。
但是,我想在 VBA 中打开一个 SVG 文件并开始操作它,但我的代码在命名空间问题上出错了。这是我的代码,它位于同一目录中工作簿的标准模块中...
Option Explicit
Sub LoadSVGFile()
'* Tools->References->Microsoft Xml, v6.0
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Dim sSVGPath As String
sSVGPath = fso.BuildPath(ThisWorkbook.Path, "Flag_of_the_United_Kingdom.svg")
If fso.FileExists(sSVGPath) Then
Dim dom As MSXML2.DOMDocument60
Set dom = New MSXML2.DOMDocument60
dom.Load sSVGPath
Debug.Assert dom.parseError = 0
'xmlns="http://www.w3.org/2000/svg
'dom.namespaces.Add "http://www.w3.org/2000/svg", "xmlns"
dom.setProperty "SelectionNamespaces", "xmlns=""http://www.w3.org/2000/svg"""
'Call dom.setProperty("SelectionLanguage", "XPath")
Dim xmlSVG As MSXML2.IXMLDOMElement
Set xmlSVG = dom.SelectSingleNode("svg")
Debug.Assert Not dom.SelectSingleNode("svg") Is Nothing
Debug.Assert Not dom.SelectSingleNode("/svg") Is Nothing
If xmlSVG Is Nothing Then End
'* rest of code follows here but is not shown
Debug.Print "'* rest of code follows here but is not shown"
End If
End Sub
当我从根元素中删除命名空间属性时,上面的代码可以工作,但这并不好,因为这些文件是由 Inkscape 等生成的。
我确定这是一个 SelectionNamespaces
属性 问题,但我无法让它工作,我相信有人可以轻松解决这个问题。
更新:谷歌搜索看起来这是预期的行为 https://support.microsoft.com/en-gb/kb/288147
据我所知,selectionNamespace 是用于选择语言 (xPath) 的 namspaces/prefixes 列表。它用于在 xPath 中的名称空间之间切换。像这样尝试:
dom.setProperty "SelectionNamespaces", "xmlns:svg=""http://www.w3.org/2000/svg"""
Dim xmlSVG As MSXML2.IXMLDOMElement
Set xmlSVG = dom.SelectSingleNode("svg:svg")
Debug.Assert Not dom.SelectSingleNode("svg:svg") Is Nothing
Debug.Assert Not dom.SelectSingleNode("/svg:svg") Is Nothing
P.S.: 自从我上次使用 Windows 以来已经有一段时间了,所以我不能保证任何东西:-(