Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3350322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:46:22+00:00 2026-05-18T01:46:22+00:00

I’m new to F# and functional and am working on some HTML parsing code.

  • 0

I’m new to F# and functional and am working on some HTML parsing code. I want to remove from a HTML document elements that match some criteria. Here I have a sequence of objects (HtmlNodes) and want to remove them from the document.

Is this idiomatic way of using pattern matching? Also as HtmlNode.Remove() has a side-effect on the original HtmlDocument object, is there any particular way of structuring the code to make the side-effect obvious or how should this be handled. You can be as pedantic as you like with the code.

open HtmlAgilityPack

let removeNodes (node : HtmlNode) = 

    let (|HiddenNodeCount|) (n : HtmlNode) =
        match n.SelectNodes("*[@style[contains(.,'visibility:hidden')]]") with
        | null -> 0
        | _ as x -> Seq.length x

    match node with
        | x when x.Name.ToLower() = "script" -> node.Remove() 
        | x when x.NodeType = HtmlNodeType.Comment -> node.Remove()
        | HiddenNodeCount x when x > 0 -> node.Remove()
        | _ -> ()

let html = "some long messy html code would be here"
let dom = new HtmlDocument(OptionAutoCloseOnEnd=true)
dom.LoadHtml(html)

let nodes = dom.DocumentNode.DescendantNodes()
nodes |> Seq.toArray |> Array.iter removeNodes
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:46:22+00:00Added an answer on May 18, 2026 at 1:46 am

    Personally, I prefer if elif else over pattern matching when you don’t have a data structure to decompose (it’s just less typing, and may also serve to differentiate between when a structure is being decomposed versus simpler case testing).

    There are some odd things in your code. The Active Pattern isn’t very helpful here for two reasons: first, its scope is limited to removeNodes so it is only used once. I’ll address the second issues later, but first I will show how I would write this by eliminating the Active Pattern and, for me at least, making the side-effects more obvious (by separating the code which tests whether a node should be removed from the code that does the removing):

    let shouldRemoveNode (node : HtmlNode) = 
        if node.Name.ToLower() = "script" then true
        elif node.NodeType = HtmlNodeType.Comment then true
        else match node.SelectNodes("*[@style[contains(.,'visibility:hidden')]]") with
             | null -> false
             | x -> Seq.length x > 0
    
    let removeNode (node: HtmlNode) = 
        if shouldRemoveNode(node) then node.Remove() else ()
    

    Notice I do use a pattern match in the visibility hidden query since I do get to match against null and bind to x otherwise (rather than binding to x, and then testing x with if else).

    The second odd thing with your Active Pattern is that you are using it for converting a node to an int, but the length you obtain isn’t immediately useful (you still need to perform a test against it). Whereas the more powerful use of an Active Pattern here would be to carve up nodes into different kinds (assuming this isn’t ad-hoc, which was may first point). So you could have:

    //expand to encompass several other kinds of nodes
    let (|Script|Comment|Hidden|Other|) (node : HtmlNode) = 
        if node.Name.ToLower() = "script" then Script
        elif node.NodeType = HtmlNodeType.Comment then Comment
        else match node.SelectNodes("*[@style[contains(.,'visibility:hidden')]]") with
             | null -> Other
             | x -> if Seq.length x > 0 then Hidden
                    else Other
    
    let removeNode (node: HtmlNode) = 
        match node with
        | Script | Comment | Hidden -> node.Remove()
        | Other -> ()
    

    Edit:

    @Pascal made the observation in the comments that shouldRemoveNode can be further condensed into one big boolean expression:

    let shouldRemoveNode (node : HtmlNode) = 
        node.Name.ToLower() = "script" || 
        node.NodeType = HtmlNodeType.Comment ||
        match node.SelectNodes("*[@style[contains(.,'visibility:hidden')]]") with
        | null -> false
        | x -> Seq.length x > 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.