David
David

Reputation: 81

XPATH remove attribute

Hi does anyone know hwo to remove an attrbute using xpath. In particular the rel attribute and its text from a link. i.e. <a href='http://google.com' rel='some text'>Link</a> and i want to remove rel='some text'.

There will be multiple links in the html i am parsing.

Upvotes: 6

Views: 6753

Answers (4)

Chad Fleming
Chad Fleming

Reputation: 1

A couple comments from 15 years ago mentioned that XPath cannot remove nodes but that is not true in 2025. XPath can remove attributes. XPath syntax can vary depending on the implementation. (Also, XSLT is probably the best solution for this)

VB.NET Framework (Very similar to C#)

Imports System.Xml

Dim xml_Node As XmlNode
Dim nodeList As XmlNodeList
Dim namespaceManager As XmlNamespaceManager
Dim xml_Document As XmlDocument

nodeList = xml_Document.SelectNodes("//*[text()='Link']", namespaceManager)
For Each xml_Node In nodeList
    xml_Node.parentNode.removeChild(xml_Node)
next xml_Node

VBA

Dim xml_Node As MSXML2.IXMLDOMNode
Dim nodeList As MSXML2.IXMLDOMNodeList
dim xml_Document as MSXML2.DOMDocument60

Set nodeList = xml_Document.SelectNodes("//*[text()='Link']")
For Each xml_Node In nodeList
    xml_Node.parentNode.removeChild xml_Node
next xml_Node

Upvotes: 0

Ashish Gupta
Ashish Gupta

Reputation: 15139

Have you already tried using Javascript for this If that is applicable in your scenario:-

var allLinks=document.getElementsByTagName("a");

for(i=0;i<allLinks.length;i++)
{
allLinks[i].removeAttribute("rel");
}

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272417

As pointed out by Oded, Xpath merely identifies XML nodes. To remove/edit XML, you need some additional tooling.

One solution is the Ant-based plugin XMLTask (disclaimer - I wrote this). It provides a simple mechanism to read an XML file, identify parts of that using XPath, and change it (including removing nodes).

e.g.

<remove path="web/servlet/context[@id='redundant']"/>

Upvotes: 0

Oded
Oded

Reputation: 499352

You can select items using xpath, but that's all it can do - it is a query language.

You need to use XSLT or an XML parser in order to remove attributes/elements.

Upvotes: 5

Related Questions