To remove an HTML element using its XPath on Linux, you typically use a command-line tool like xmllint (from libxml2) or xmlstarlet, or a scripting language like Python with libraries such as lxml. Here’s how you can do it with each method:
xmlstarletxmlstarlet is a powerful command-line tool for editing XML/HTML files.
xmlstarlet (if not already installed):bashsudo apt-get install xmlstarlet
bashxmlstarlet ed -d "//xpath/to/element" input.html > output.html
//xpath/to/element with your actual XPath.input.html is your source file, and output.html is the result.lxmlIf you prefer scripting, Python’s lxml library is a great choice.
lxml:bashpip install lxml
pythonfrom lxml import etree # Load the HTML file tree = etree.parse("input.html") # Find the element using XPath element = tree.xpath("//xpath/to/element") if element: element[0].getparent().remove(element[0]) # Save the result tree.write("output.html", pretty_print=True)
//xpath/to/element with your XPath.python script.py.xmllintxmllint is part of the libxml2 package and can be used for simple edits.
libxml2 (if not already installed):bashsudo apt-get install libxml2-utils
bashxmllint --shell input.html <<EOF xpath //xpath/to/element delete save output.html EOF
lxml is the