Remove a parent node and all children based on text value in child

落花浮王杯 提交于 2019-12-11 03:49:56

问题


I am trying to read an xml file with vbscript and if the ATTRIBUTE_DATA contains V-3008, I want to delete/remove the whole vuln .... vuln that contains it. - I have tried all different XPath variations and can't come up with the one that will do that. I have had it deleting the entire row that contains it but that is no good. I did search this site first and there isn't a whole lot on this topic: https://stackoverflow.com/search?q=xpath+vbscript+remove+node

Here is some basic code I have started. Thank you.

Set xmlDoc = _
CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("C:\Users\somedude\Desktop\xml-development\myfile.xml")

strVulid = "V-3008"
xpath  = "//CHECKLIST/VULN/STIG_DATA/ ATTRIBUTE_DATA[text() = '" & strVulid & "']/ATTRIBUTE_DATA "

For Each n In XMLDoc.SelectNodes(xpath)
    n.parentNode.removeChild(n)
Next

xmlDoc.Save "C:\Users\somedude\Desktop\xml-development\myfile.xml" 
msgbox "done"

Here Is the example XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CHECKLIST>
<SV_VERSION>DISA STIG Viewer : 1.2.0</SV_VERSION>
 <ASSET>
  <ASSET_TYPE>Computing</ASSET_TYPE>
  <HOST_NAME>HOST NAME</HOST_NAME>
  <HOST_IP>IP</HOST_IP>
  <HOST_MAC>MAC</HOST_MAC>
  <HOST_GUID></HOST_GUID>
  <TARGET_KEY></TARGET_KEY>
  <ASSET_VAL>
    <AV_NAME xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">ROLE</AV_NAME>
    <AV_DATA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Role</AV_DATA>
  </ASSET_VAL>
</ASSET>
<STIG_INFO>
  <STIG_TITLE>Windows 7 Security Technical Implementation Guide</STIG_TITLE>
</STIG_INFO>
<VULN>
  <STIG_DATA>
    <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>V-3008</ATTRIBUTE_DATA>
  </STIG_DATA>    
  <STIG_DATA>
    <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>medium</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Group_Title</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>IPSec VPN is not configured as a tunnel type VPN.</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Rule_ID</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>SV-3008r1_rule</ATTRIBUTE_DATA>
</STIG_DATA>    
</VULN>  
<VULN>
<STIG_DATA>
    <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>V-1234</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>medium</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Group_Title</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>Some Text</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Rule_ID</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>Srrgs1_rule</ATTRIBUTE_DATA>
</STIG_DATA>    
</VULN>
<VULN>
<STIG_DATA>
    <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>V-5678</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>medium</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Group_Title</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>Some Other Text</ATTRIBUTE_DATA>
</STIG_DATA>    
<STIG_DATA>
    <VULN_ATTRIBUTE>Rule_ID</VULN_ATTRIBUTE>
    <ATTRIBUTE_DATA>dSrrdafgs1_rule</ATTRIBUTE_DATA>
</STIG_DATA>    
</VULN> 
</CHECKLIST>

回答1:


You want the xpath to return <VULN> elements that satisfy the criteria :

......

strVulid = "V-3008"
xpath  = "//CHECKLIST/VULN[STIG_DATA/ATTRIBUTE_DATA = '" & strVulid & "']"

For Each n In XMLDoc.SelectNodes(xpath)
    n.parentNode.removeChild(n)
Next

......

Above xpath return <VULN> having descendant element <ATTRIBUTE_DATA> exactly equals "V-3008".

Use contains() if you meant to match <ATTRIBUTE_DATA> value partially :

xpath  = "//CHECKLIST/VULN[STIG_DATA/ATTRIBUTE_DATA[contains(.,'" & strVulid & "')]]"


来源:https://stackoverflow.com/questions/30316813/remove-a-parent-node-and-all-children-based-on-text-value-in-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!