Pattern match on HXT does not work as expected

45 views Asked by At

For example, I have a div containing a few ps and others, and I want to map the ps with show.

defShow :: NTree XNode -> String
defShow (NTree (XTag div' _) contents)
  | show div' == "div" = intercalate "\n" $ map defShow contents
defShow p@(NTree (XTag p' []) _)
  | show p' == "p" = show p
defShow x = error $ show x

But with testing data as following, this does not work. I mean the whole node, not just the p node.

NTree (XTag "div" [NTree (XAttr "class") [NTree (XText "refsect2") []]])
      [ NTree (XText "\n") []
      , NTree (XTag "p" []) [SOME_TEXT_NODE] ]
1

There are 1 answers

0
Li-yao Xia On

Don't use show in the logic of your program. You can use mkName to convert a String to a QName.

defShow :: NTree XNode -> String
defShow (NTree (XTag div' _) contents)
  | div' == mkName "div" = intercalate "\n" $ map defShow contents
defShow p@(NTree (XTag p' []) _)
  | p' == mkName "p" = show p
defShow x = ""