# Unwanted extra spaces at HTML tag boundaries when using ImportString

What causes the following unexpected behavior?

In[1]:= ImportString["a<span>b</span>c", "HTML"]
Out[1]= "a b c"

In[2]:= ImportString["a<foo>b</foo>c", "HTML"]
Out[2]= "a bc"


In both cases, I would expect the returned string to be "abc".

What is the best way to work around it?

I don't know what causes the issue (which looks like a bug to me), but I generally find the best way to deal with exerting control over Import not doing quit the right thing with HTML is to use the "XMLObject" element and use ordinary Mathematica functions on it, like so:

In[1]:= ImportString@
ExportString[
ImportString["a<span>b</span>c", {"HTML", "XMLObject"}] /.
XMLElement[
Except["html" | "body" | "p" | "li" | "table" | "tr" | "td" |
"br" | "ul" | "li"], {___}, {stuff___}] :> stuff, "XML"]
Out[1]= "abc"


It's kinda cumbersome to round-trip like that, but on the upside it's very flexible.

EDITed to strip out a wider range of tags.

• Unfortunately, it doesn't for for other known tags (e.g. <i>...</i>) – ybeltukov Nov 14 '15 at 23:10
• @ybeltukov yeah, that's an issue; it wouldn't be so hard to fix, but I don't know which tags are (or, perhaps more usefully, aren't) supposed to work that way. Google University time, I guess. – Pillsy Nov 14 '15 at 23:19

jSoupLink can be used as a workaround, like this:

<< jSoupLink
"<div id=\"container\">" <> html <> "</div>", "#container", "text"
]

html2text["a<span>b</span>c"]
(* Out: abc *)

html2text["a<foo>b</foo>c"]
(* Out: abc *)


One can import raw text and use this example to remove HTML tags

StringReplace[  #, "<" ~~ Except[">"] .. ~~ ">" -> ""] &@
ImportString["a<bar>b</bar>c", {"HTML", "Source"}]
(* "abc" *)