使用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"]
示例XML不這裏... – florent