问题
I'm looking to parse an xhtml document with Microsoft.XMLHTTP with XPATH in VBScript. I have the following xhtml document structure. How would I get an array of the urls?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Local index</title>
</head>
<body>
<table>
<tr>
<td>
<a href="url1.html">url1</a><br/>
<a href="url2.html">url2</a><br/>
<a href="url3.html">url3</a>
</td><td>
<a href="url1-1.html">url1-1</a><br/>
<a href="url2-1.html">url2-1</a><br/>
<a href="url3-1.html">url3-1</a>
</td>
</tr>
</table>
</body>
</html>
回答1:
Are you sure you need to use the antiquated program id Microsoft.XMLHTTP
? These days both MSXML 3 as well as MSXML 6 are part of the OS respectively supported service packs with anything since Windows XP.
As for using XPath and MSXML 3, here is an example:
Dim doc
Set doc = CreateObject("Msxml2.DOMDocument.3.0")
doc.validateOnParse = False
doc.resolveExternals = False
If doc.load("file.xml") Then
doc.setProperty "SelectionLanguage", "XPath"
doc.setProperty "SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml'"
For Each link In doc.selectNodes("//xhtml:a")
WScript.Echo(link.getAttribute("href") & ": " & link.text)
Next
Else
WScript.Echo(doc.parseError.reason)
End If
来源:https://stackoverflow.com/questions/9822520/parsing-xhtml-with-xpath-using-microsoft-xmlhttp-in-vbscript