# Parsing xhtml with libxml

**URL:** <https://rubytalk.org/t/parsing-xhtml-with-libxml/23357>\
**Category:** ruby-talk\
**Created:** [16 December 2005 23:18 UTC](https://rubytalk.org/t/parsing-xhtml-with-libxml/23357 "2005-12-16T23:18:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Smirl](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Jon\_Smirl](https://rubytalk.org/u/Jon_Smirl)\
**Post date:** [16 December 2005 23:18 UTC](https://rubytalk.org/t/parsing-xhtml-with-libxml/23357/1 "2005-12-16T23:18:54Z")

</div>

If you get errors complaining of undefined entities like &nbsp; when  
parsing xhtml it means you need to install the DTD for xhtml 1.0 or  
1.1.

Example of a doctype for xhtml 1.1:  
\<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  
"[http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt](http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt);

You want to install the DTDs locally following the model in /etc/xml.  
If you don't libxml will fetch the DTD from [www.w3.org](http://www.w3.org) each time you  
parse a document. Needing to install these DTDs was not obvious to me  
and should be part of the documentation. There a rpm for xhtml 1.0 -  
"xhtml1-dtds-1.0-7". I couldn't find one for xhtml 1.1 so I downloaded  
it piecemeal from w3.org.

Installing the DTD does not automatically turn on validation. If you  
want to validate you need to turn it on:  
XML::Parser::default\_validity\_checking = TRUE

XML::Parser::default\_load\_external\_dtd controls the loading of the  
'external subset' (the definition for the character entities like  
&amp;. It is defaulted to TRUE.

XML::Parser::default\_load\_external\_dtd is broken. This fixes it.

Index: ruby\_xml\_parser.c

> **···**
>
> ==========================================================  
> RCS file: /var/cvs/xml-tools/libxml-ruby/ruby\_xml\_parser.c,v  
> retrieving revision 1.1.1.1  
> diff -r1.1.1.1 ruby\_xml\_parser.c  
> 274c274  
> \< if (xmlSubstituteEntitiesDefaultValue)  
> ---
> 
> > &nbsp;&nbsp;if (xmlLoadExtDtdDefaultValue)
> 
> 916c916  
> \< ruby\_xml\_parser\_default\_load\_external\_dtd\_set, 0);  
> ---
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_xml\_parser\_default\_load\_external\_dtd\_get, 0);
> 
> 918c918  
> \< ruby\_xml\_parser\_default\_load\_external\_dtd\_get, 1);  
> ---
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_xml\_parser\_default\_load\_external\_dtd\_set, 1);
> 
> Sam's patches for libxml are also needed:  
> [http://www.intertwingly.net/blog/2005/11/05/Patch-for-libxml2s-Ruby-binding](http://www.intertwingly.net/blog/2005/11/05/Patch-for-libxml2s-Ruby-binding)

---

<div class="post-metadata">

**Author:** ![Eero\_Saynatkari1](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eero\_Saynatkari1](https://rubytalk.org/u/Eero_Saynatkari1)\
**Post date:** [16 December 2005 23:42 UTC](https://rubytalk.org/t/parsing-xhtml-with-libxml/23357/2 "2005-12-16T23:42:41Z")

</div>

Jon Smirl wrote:

> If you get errors complaining of undefined entities like &nbsp; when  
> parsing xhtml it means you need to install the DTD for xhtml 1.0 or  
> 1.1.
> 
> Example of a doctype for xhtml 1.1:  
> \<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  
> "[http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt](http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt);
> 
> \<snip explanation & code due to ruby-forum.com /\>
> 
> Sam's patches for libxml are also needed:  
> [Patch for libxml2's Ruby binding](http://www.intertwingly.net/blog/2005/11/05/Patch-for-libxml2s-Ruby-binding)

Thank you for this!

E

> **···**
>
> --  
> This document is NOT valid XHTML 1.0!
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [17 December 2005 00:12 UTC](https://rubytalk.org/t/parsing-xhtml-with-libxml/23357/3 "2005-12-17T00:12:45Z")

</div>

Thanks for that. I've been gathering up problems and patches in a quiet sort of way, but I'm not sure at the moment what's happening with the project. I'm planning to get proactive this week and see if we can at least get these issues sorted and the patches I have (including yours and Sam's) in.

Thanks,  
Ross

> **···**
>
> On Fri, 16 Dec 2005 23:18:54 -0000, Jon Smirl \<jonsmirl@gmail.com\> wrote:
> 
> > If you get errors complaining of undefined entities like &nbsp; when  
> > parsing xhtml it means you need to install the DTD for xhtml 1.0 or  
> > 1.1.
> 
> --  
> Ross Bamford - rosco@roscopeco.remove.co.uk
