XML character dereferencer

Just in case you ever need one:

xmlCharDeref :: String -> String
xmlCharDeref [] = []
xmlCharDeref ('&':'#':'x':r) = let
        (digits, remainder) = span (/= ';') r
        c = chr (read ("0x" ++ digits))
    in
        c : xmlCharDeref (tail remainder)
xmlCharDeref ('&':'#':r) = let
        (digits, remainder) = span (/= ';') r
        c = chr (read digits)
    in
        c : xmlCharDeref (tail remainder)
xmlCharDeref (c:r) = c : xmlCharDeref r

In ghci:

*Foo> xmlCharDeref "hello there"
"hello there"
*Foo> xmlCharDeref "hello there"
"hello there"
*Foo> xmlCharDeref "hello2there"
"hello2there"
Share

2 Comments

  1. How about:

    newtype XMLString = XMLString String
    
    xmlCharDeref :: String -> XMLString
    

    Now there’s no way you can accidentally encode twice. :-)

  2. @Chris: Except of course that the type would be

    xmlCharDeref :: XMLString -> String

    In any case it’s hardly worth doing for the particular case where I had use for this function.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>