Search code examples
xmlasp-classic

ASP XML select node by specific node value?


<fruits>
  <fruit>apple</fruit>
  <fruit>pear</fruit>
  <fruit>grape</fruit>
</fruits>

I want to select node by using node's value "apple" and remove node

when I googled it, there are only ways to get node's value


Solution

  • Try selecting fruit nodes (using XPath) where the inner text contains 'apple'. Once obtained, enumerate through the results informing the parent node (fruits) to remove the selected child node(s) that were returned by the XPath selection.

    For example, here's a simplified example that assumes the XML file is loaded locally (via file I/O), and whose results are saved to a new file (for review):

    With Server.CreateObject("Microsoft.XMLDOM")
        .async = False 
        .load("data.xml")
        Dim apple, apples : Set apples = .documentElement.selectNodes("/fruits/fruit[text()='apple']")
        For Each apple In apples
            apple.parentNode.removeChild(apple)
        Next
        .save("data2.xml")
    End With
    

    Hope this is helpful.