# REXML + DTD example

**URL:** <https://www.ruby-forum.com/t/rexml-dtd-example/92815>\
**Category:** Ruby\
**Created:** [April 24, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/rexml-dtd-example/92815 "2007-04-24T14:42:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [April 24, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/rexml-dtd-example/92815/1 "2007-04-24T14:42:14Z")

</div>

Hello Rubyists,

I’d like to have a custom made XML configuration file parsed with a  
custom made DTD file. However, the documentation at  
[http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/DTD.html](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/DTD.html)  
doesn’t help me a lot. Is there anybody who could paste a simple 4-liner  
example scenario about how this would be done?

Thanks a lot in advance

-carp

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [April 24, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/rexml-dtd-example/92815/2 "2007-04-24T15:05:46Z")

</div>

On 24.04.2007 14:42, carp \_\_ wrote:

> I’d like to have a custom made XML configuration file parsed with a  
> custom made DTD file. However, the documentation at  
> [http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/DTD.html](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/DTD.html)  
> doesn’t help me a lot. Is there anybody who could paste a simple 4-liner  
> example scenario about how this would be done?

Did you look at  
[http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)  
reachable from [http://www.germane-software.com/software/rexml/](http://www.germane-software.com/software/rexml/) ?

robert

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [April 25, 2007, 1:21am UTC](https://www.ruby-forum.com/t/rexml-dtd-example/92815/3 "2007-04-25T01:21:17Z")

</div>

carp \_\_ wrote:

> Hello Rubyists,
> 
> I’d like to have a custom made XML configuration file parsed with a  
> custom made DTD file. However, the documentation at  
> [http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/DTD.html](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/DTD.html)  
> doesn’t help me a lot. Is there anybody who could paste a simple 4-liner  
> example scenario about how this would be done?

RexML can’t do external DTDs, and the author told me it won’t ever,  
as he believes that the syntax is too evil to even contemplate.

When I needed to do it, on Windows systems having cygwin installed,  
I just opened the XML file using a pipe from xmllint, which was  
launched with the right parameters to make it expand the entities  
from an external DTD. The main awkwardness comes from the fact that  
xmllint looks for unqualified DTD files in the same directory as  
the XML file, so you need to extract the directory pathname from  
the file’s basename and cd to that directory first:

cmd = “sh -c ‘cd #{dir}; xmllint --loaddtd --noent - \<#{base}’”  
infile = popen(cmd)

Best of luck.

Clifford H…

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [April 25, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/rexml-dtd-example/92815/4 "2007-04-25T15:15:58Z")

</div>

carp \_\_ wrote:

> Thanks for that hint, Clifford.

You’re welcome. It took a bit of headscratching before  
I convinced myself we had to go that way, but seeing  
that we had a type of XML file that needed to be expanded  
with two different sets of entities (for code generation  
and for documentation generation), it was only pragmatic.

Clifford.

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [April 25, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/rexml-dtd-example/92815/5 "2007-04-25T12:01:52Z")

</div>

Clifford H. wrote:

> RexML can’t do external DTDs, and the author told me it won’t ever,  
> as he believes that the syntax is too evil to even contemplate.
> 
> When I needed to do it, on Windows systems having cygwin installed,  
> I just opened the XML file using a pipe from xmllint, which was  
> launched with the right parameters to make it expand the entities  
> from an external DTD. The main awkwardness comes from the fact that  
> xmllint looks for unqualified DTD files in the same directory as  
> the XML file, so you need to extract the directory pathname from  
> the file’s basename and cd to that directory first:
> 
> cmd = “sh -c ‘cd #{dir}; xmllint --loaddtd --noent - \<#{base}’”  
> infile = popen(cmd)

Thanks for that hint, Clifford. I did it with xmllint now - a bit ugly  
as it makes the script less portable, but well … better than nothing I  
guess. Thanks again!

-carp
