Olli
Olli

Reputation: 89

Delete Tags by Matching Value on XML File

I try to delete each tag “a” if the attribute value matches “table.xml”. For this I use vbscript. Can anyone help me with this or give me tip? Thank you

XML file bevor editing

<?xml version="1.0" encoding="UTF-8"?>
  <html>
    <head>
      <title>title</title>
    </head>
    <body>
      <h1>title h1</h1>
      <p>This is a test.</p>
      <p>This is a <a href="list.xml">test</a>.</p>
      <ul>
        <li>List one</li>
        <li>List two <a href="table.xml">test</a></li>
      </ul>
      <p>This is a <a href="table.xml">test</a>.</p>
    </body>
  </html>

XML file hove it shot look after deleting tag a with attribute value “table.xml”.

<?xml version="1.0" encoding="UTF-8"?>
  <html>
    <head>
      <title>title</title>
    </head>
    <body>
      <h1>title h1</h1>
      <p>This is a test.</p>
      <p>This is a <a href="list.xml">test</a>.</p>
      <ul>
        <li>List one</li>
        <li>List two test</li>
      </ul>
      <p>This is a test.</p>
    </body>
  </html>

Upvotes: 0

Views: 104

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

As the linked answer does not deal with finding elements 'everywhere' ("//a") and the text "test" is special, because it 'belongs' to the <a> node and its parent:

Option Explicit

Dim sFSpec : sFSpec   = "..\data\39911374.xml"
Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
oXML.setProperty "SelectionLanguage", "XPath"
oXML.async = False
oXML.load sFSpec
If 0 = oXML.parseError Then
   WScript.Echo oXML.xml
   Dim sXPath : sXPath     = "//a[@href=""table.xml""]"
   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, ndPar, sTmp
      For Each ndCur In ndlFnd
          Set ndPar = ndCur.parentNode
          sTmp = ndPar.text
          ndPar.removeChild ndCur
          ndPar.text = sTmp
      Next
      WScript.Echo "-----------------"
      WScript.Echo oXML.xml
   End If
Else
   WScript.Echo oXML.parseError.reason
End If

output:

cscript 39911374.vbs
<?xml version="1.0"?>
<html>
        <head>
                <title>title</title>
        </head>
        <body>
                <h1>title h1</h1>
                <p>This is a test.</p>
                <p>This is a <a href="list.xml">test</a>.</p>
                <ul>
                        <li>List one</li>
                        <li>List two <a href="table.xml">test</a></li>
                </ul>
                <p>This is a <a href="table.xml">test</a>.</p>
        </body>
</html>

found 2 nodes for //a[@href="table.xml"]
-----------------
<?xml version="1.0"?>
<html>
        <head>
                <title>title</title>
        </head>
        <body>
                <h1>title h1</h1>
                <p>This is a test.</p>
                <p>This is a <a href="list.xml">test</a>.</p>
                <ul>
                        <li>List one</li>
                        <li>List two test</li>
                </ul>
                <p>This is a test.</p>
        </body>
</html>

Upvotes: 1

Related Questions