使用 vbscript 有条件地从 XML 中删除节点 [英] conditionally remove nodes from XML using vbscript

查看:21
本文介绍了使用 vbscript 有条件地从 XML 中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xml 文件,用于将数据解析为 HTML 文件.我正在使用 vbscript 来做到这一点.在解析数据之前,我需要有条件地删除文件中的一些节点.我的 HTML 表单上有一个日期,需要用来与 XML 文件中的日期进行比较.如果日期超出范围,那么我想删除该节点及其下的任何子节点.

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.

这是一个示例 XML:

Here is a sample XML:

在上面的示例中,如果任何子节点的exp"值小于我表单上的日期,则应将其删除.如果它下面有一个子节点,那么它也应该被删除.所以如果我的表单上的日期是 12/5/12,那么我的第一个O"节点应该和它下面的子节点一起被删除.所有节点都有一个日期,所以我必须查看每个节点.该文件可以像这样小,也可以有许多附加节点.任何人都可以帮助我指出正确的方向吗?同样,这需要使用 vbscript 来完成.

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.

推荐答案

使用 XPath 查找有趣的"节点并使用 .removeChild 将它们删除:

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

输出:

======================================================================
<?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>

======================================================================

手动过滤很糟糕,但是

  1. 它会为您不太实用的日期格式派上用场
  2. 我无法让 XPath 接受 <= 响应.&lt; 在我的搜索表达式中
  1. it will come handy for your not-really-usefull date format
  2. I can't get XPath to accept <= resp. &lt; in my search expression

这个更好看的代码片段:

This 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
 ...

删除数独节点,但

...
Dim sXPath : sXPath     = "/addons/addon[@date &lt; """ & sDate & """]"
...

投掷

msxml6.dll: Unexpected character in query string.
/addons/addon[@date -->&<--lt; "2012-08-31"]

这篇关于使用 vbscript 有条件地从 XML 中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆