使用 XPath 查找“有趣的”节点并使用 .removeChild 删除它们:
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName("..\data\01.xml")
Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
Dim sDate : sDate = "2012-08-31"
oXML.setProperty "SelectionLanguage", "XPath"
oXML.async = False
oXML.load sFSpec
If 0 = oXML.parseError Then
WScript.Echo oXML.xml
WScript.Echo "-----------------"
Dim sXPath : sXPath = "/addons/addon[@date=""" & sDate & """]"
Dim ndlFnd : Set ndlFnd = oXML.selectNodes(sXPath)
If 0 = ndlFnd.length Then
WScript.Echo sXPath, "not found"
Else
WScript.Echo "found", ndlFnd.length, "nodes for", sXPath
Dim ndCur
For Each ndCur In ndlFnd
ndCur.parentNode.removeChild ndCur
Next
End If
WScript.Echo "-----------------"
WScript.Echo oXML.xml
Else
WScript.Echo oXML.parseError.reason
End If
输出:
======================================================================
<?xml version="1.0"?>
<addons>
<addon id="TicTacToe" date="2012-11-05">
<requires>
<import addon="xbmc.python" version="1.0"/>
</requires>
</addon>
<addon id="Sudoku" date="2012-08-31">
<requires>
<import addon="xbmc.python" version="1.0"/>
</requires>
</addon>
<addon id="Doom" date="1953-04-13">
<requires>
<import addon="xbmc.python" version="1.0"/>
</requires>
</addon>
<addon id="Muehle" date="2012-10-18">
<requires>
<import addon="xbmc.python" version="1.0"/>
</requires>
</addon>
</addons>
-----------------
found 4 nodes for /addons/addon
filtering for dtX <= 31.08.2012
-----------------
<?xml version="1.0"?>
<addons>
<addon id="TicTacToe" date="2012-11-05">
<requires>
<import addon="xbmc.python" version="1.0"/>
</requires>
</addon>
<addon id="Muehle" date="2012-10-18">
<requires>
<import addon="xbmc.python" version="1.0"/>
</requires>
</addon>
</addons>
======================================================================
手动过滤很烂,但是
- 它会派上用场你不是真的有用的日期格式
- 我无法让 XPath 接受
<=
响应。<
在我的搜索表达式中
这个更好看的代码片段:
...
Dim sXPath : sXPath = "/addons/addon[@date=""" & sDate & """]"
Dim ndlFnd : Set ndlFnd = oXML.selectNodes(sXPath)
If 0 = ndlFnd.length Then
WScript.Echo sXPath, "not found"
Else
WScript.Echo "found", ndlFnd.length, "nodes for", sXPath
Dim ndCur
For Each ndCur In ndlFnd
ndCur.parentNode.removeChild ndCur
Next
End If
...
删除数独节点,但
...
Dim sXPath : sXPath = "/addons/addon[@date < """ & sDate & """]"
...
投掷
msxml6.dll: Unexpected character in query string.
/addons/addon[@date -->&<--lt; "2012-08-31"]