我正在尝试使用VBScript来解析从Microsoft Excel导出的XML电子表格文件。我开始尝试计算<Row>
元素的数量。但是脚本总是返回0.我做错了什么?
这是我的VBScript文件:
Set oXML = CreateObject("Microsoft.XMLDOM")
oXML.aSync = false
oXML.SetProperty "SelectionLanguage", "XPath"
oXML.SetProperty "ServerHTTPRequest", True
oXML.validateOnParse = False
oXML.resolveExternals = False
oXML.Load "_test_.xml"
MsgBox oXML.SelectNodes("//Row").length ' Return 0
WScript.Quit
' Looping through all nodes works fine
Set nodes = oXML.selectNodes("//*")
For i = 0 to nodes.length -1
Msgbox nodes(i).nodeName
Next
这是XML文件:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="2" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">First Row</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">Second Row</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
答案 0 :(得分:4)
由于存在多个名称空间,因此必须为XPATH定义这些名称空间。即使对于默认命名空间,也必须这样做。如果没有,您无法使用XPATH从命名空间中获取具体元素。这就是为什么//*
可以工作但//Row
不起作用的原因,因为XPATH不知道哪个命名空间Row
属于。
使用setProperty Method设置名称空间。另请参阅Second-Level DOM Properties和SelectionNamespaces Property。
你的例子:
Set oXML = CreateObject("Microsoft.XMLDOM")
oXML.aSync = false
oXML.SetProperty "SelectionLanguage", "XPath"
oXML.SetProperty "ServerHTTPRequest", True
oXML.validateOnParse = False
oXML.resolveExternals = False
oXML.setProperty "SelectionNamespaces", "xmlns:d=""urn:schemas-microsoft-com:office:spreadsheet""" & _
" xmlns:o=""urn:schemas-microsoft-com:office:office""" & _
" xmlns:x=""urn:schemas-microsoft-com:office:excel""" & _
" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""" & _
" xmlns:html=""http://www.w3.org/TR/REC-html40"""
oXML.Load "_test_.xml"
MsgBox oXML.SelectNodes("//d:Row").length ' Return 2
' Looping through all rows in Table
Set nodes = oXML.selectNodes("//d:Table//*")
For i = 0 to nodes.length -1
Msgbox nodes(i).nodeName
Next
在该示例中,我使用d
为默认命名空间添加了前缀。