Reputation: 13
I have the following HTML:
<div id="main-body">
<div id="answerform">
// ... a few children here i want
<form id=mwanser">
</form>
</div>
</div>
I want everything in the main-body, except what is in the form tag... (and don't want the form tag itself too)....
I tried with XPath a lot but i didn't succeed.
I'm working with C# and HTMLAgilityPack, maybe there's a way to navigate to the child and delete it instead? The first option for me must be XPath too.
Upvotes: 1
Views: 297
Reputation: 991
I think I have achieved this using this answer:
How to remove elements from xml using xslt with stylesheet and xsltproc?
This is done using plain XSL so I don't know if it will help with HTMLAgilityPack.
Here's the XSL that I used:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="form"/>
</xsl:stylesheet>
EDIT: just had a quick look at the HtmlAgilityPack code and found this that should have worked:
HtmlAgilityPack.HtmlNode node;
node = htmldoc.DocumentNode.SelectSingleNode("//form");
node.ParentNode.RemoveChild(node, false);
but for some reason it only deleted the opening tag.
Upvotes: 1
Reputation: 24826
With XPath if you select a node you obtain the node and every descendant in it. What you can do is select the children directly but the unwanted node. Taking your sample as a test, you can write:
"/*/*/*[not(self::form)]"
Upvotes: 0