# Preventing "collapse" of HTML tags during XML parse

**URL:** <https://www.ruby-forum.com/t/preventing-collapse-of-html-tags-during-xml-parse/113420>\
**Category:** Ruby\
**Created:** [August 30, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/preventing-collapse-of-html-tags-during-xml-parse/113420 "2007-08-30T23:11:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [August 30, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/preventing-collapse-of-html-tags-during-xml-parse/113420/1 "2007-08-30T23:11:05Z")

</div>

I’m trying to avoid having

turn into

But this is exactly what happens to me in both XmlSimple and REXML:

irb(main):028:0\> print  
XmlSimple.xml\_out(XmlSimple.xml\_in("\<textarea id=“foo”\>\</tex  
tarea\>"))

The problem is that

doesn’t render in a browser.

Does anyone know of a way to avoid this in either library? Or, should I  
be doing this another way?

Thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [August 30, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/preventing-collapse-of-html-tags-during-xml-parse/113420/2 "2007-08-30T23:30:54Z")

</div>

Rob H. wrote:

> I’m trying to avoid having
> 
> turn into
> 
> But this is exactly what happens to me in both XmlSimple and REXML:
> 
> irb(main):028:0\> print  
> XmlSimple.xml\_out(XmlSimple.xml\_in("\<textarea id=“foo”\>\</tex  
> tarea\>"))
> 
> The problem is that
> 
> doesn’t render in a browser.
> 
> Does anyone know of a way to avoid this in either library? Or, should I  
> be doing this another way?
> 
> Thanks,  
> Rob

HTML is not an XML language, it is an SGML language. Use an HTML parser  
instead of an XML parser. For example Hpricot.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [August 31, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/preventing-collapse-of-html-tags-during-xml-parse/113420/3 "2007-08-31T15:48:48Z")

</div>

On 8/30/07, Rob H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to avoid having
> 
> turn into
> 
> But this is exactly what happens to me in both XmlSimple and REXML:

and are equivalent in the XML spec, so this  
is correct behavior.

> Does anyone know of a way to avoid this in either library? Or, should I  
> be doing this another way?

If you’re using REXML, you might investigate the last argument to  
write():

[http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Element.html#M002971](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Element.html#M002971)

ie\_hack: Internet Explorer is the worst piece of crap to have ever  
been written, with the possible exception of Windows itself. Since IE  
is unable to parse proper XML, we have to provide a hack to generate  
XML that IE’s limited abilities can handle. This hack inserts a space  
before the /\> on empty tags. Defaults to false

HTH,  
Keith
