2012-12-05 30 views
0

我有一個xml文件,用於將數據解析爲HTML文件。我使用VBScript來做到這一點。在解析數據之前,我需要有條件地刪除文件中的一些節點。我在我的HTML表單上有一個日期,我需要用它來比較XML文件中的日期。如果日期超出範圍,那麼我想刪除該節點及其下的任何子節點。使用vbscript有條件地從XML中刪除節點

這裏是一個示例XML: XML Data

在上面的例子中,如果任何一個子節點有一個「EXP」值是小於我的表格上的日期,那麼它應該被刪除。如果它下面有一個子節點,那麼它也應該被刪除。所以如果我的表單上的日期是2012年5月12日,那麼我的第一個「O」節點應該與它下面的子節點一起被刪除。所有的節點都有一個日期,所以我必須看看每一個。該文件可以像這樣小或者有許多其他節點。任何人都可以幫助我指出正確的方向嗎?再次,這需要使用VBScript來完成。

+0

示例XML不這裏... – florent

回答

1

使用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> 

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

手動過濾很爛,但

  1. 它會來爲你的不是真正有用的日期格式得心應手
  2. 我不能讓XPath接受<=或者&lt;在我的搜索表達式

這更好看的代碼片段:

... 
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"]