Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HXT -- what is "deep"?

Tags:

xml

haskell

hxt

I'm putting in a lot of time trying to figure out how to use HXT. I keep coming against examples using deep. What does deep do?

For example, this code has the following:

atTag tag = deep (isElem >>> hasName tag)

Another example:

-- case-insensitive tag matching
atTagCase tag = deep (isElem >>> hasNameWith ((== tag') . upper . localPart))
  where tag' = upper tag
        upper = map toUpper
like image 910
Muchin Avatar asked Feb 27 '23 02:02

Muchin


1 Answers

http://hackage.haskell.org/packages/archive/hxt/latest/doc/html/Control-Arrow-ArrowTree.html#v:deep

deep :: Tree t => a (t b) c -> a (t b) cSource

recursively searches a whole tree for subtrees, for which a predicate holds. The search is performed top down. When a tree is found, this becomes an element of the result list. The tree found is not further examined for any subtress, for which the predicate also could hold. See multi for this kind of search.

example: deep isHtmlTable selects all top level table elements in a document (with an appropriate definition for isHtmlTable) but no tables occuring within a table cell.

You could find the documentation given a function name or type signature with Hoogle or Hayoo!


Basically, if the XML tree is like

<p>
    <strong id="a">
       <em id="b">
          <strong id="c">
             foo
          </strong>
       </em>
    </strong>
    <ins id="d">
       <strong id="e">
          bar
       </strong>
       <em id="f">
          baz
       </em>
    </ins>
</p>

deep (isElem >>> hasName "strong") tree will return a list for

<strong id="a">
<strong id="e">

because we can find these two <strong>s when walking into the tree, while (isElem >>> hasName tag) tree will return an empty list because the root of the tree is a <p>, not a <strong>

like image 191
kennytm Avatar answered Mar 06 '23 22:03

kennytm