Reputation: 188
I have an xml file that I use to parse data into an HTML file. I'm using vbscript to do this. Before parsing in the data I need to conditionally remove some of the nodes in my file. I have a date on my HTML form that I need to use to compare to dates in the XML file. If the dates are outside the range, then I want to remove the node and any child node under it.
Here is a sample XML:
In the above example, if any of the child nodes has an "exp" value that's less than the date on my form, then it should be removed. If there's a child node under it, then it should also be removed. So if the date on my form is 12/5/12, then my first "O" node should be deleted along with the child node under it. All of the nodes have a date so i have to look at each one. The file could be as small as this or have many additional nodes. Can anyone help point me in the right direction? Again, this needs to be done using vbscript.
Upvotes: 0
Views: 3372
Reputation: 38745
Use XPath to find the 'interesting' nodes and .removeChild to zap them:
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
output:
======================================================================
<?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>
======================================================================
The manual filtering sucks, but
<=
resp. <
in my search expressionThis much better looking code fragment:
...
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
...
deletes the Sudoku node, but
...
Dim sXPath : sXPath = "/addons/addon[@date < """ & sDate & """]"
...
throws
msxml6.dll: Unexpected character in query string.
/addons/addon[@date -->&<--lt; "2012-08-31"]
Upvotes: 1