# REXML cannot handle apostrophes?

**URL:** <https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667>\
**Category:** Ruby\
**Created:** [April 24, 2008, 2:20am UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667 "2008-04-24T02:20:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 24, 2008, 2:20am UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667/1 "2008-04-24T02:20:38Z")

</div>

I have this line in my XML file that was created by REXML

And I simply cannot access that element using REXML, at all, in any  
way, no matter what, as far as I can tell…

I have tried escaping the apostrophes, I have tried converting them to  
', I have tried both directly accessing the element and using  
XPath, nothing works.

Does anybody have any tricks for how to access elements that have  
attributes with apostrophes in their names? Thanks!

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 24, 2008, 2:34am UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667/2 "2008-04-24T02:34:39Z")

</div>

I believe this is not valid XML. You’d need to covert single ’ to "  
before you can use XML parsers to look at your format as XML. Post your  
XML file some place like [http://www.stg.brown.edu/service/xmlvalid/](http://www.stg.brown.edu/service/xmlvalid/) and  
see if it’s valid.

-ab

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 24, 2008, 2:41am UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667/3 "2008-04-24T02:41:05Z")

</div>

I finally got it to work with this wierd thing:

doc.root.elements[“tv\_show[@name=”#{s}"]/episode[@name="#{n}"]"]

However, you are right, it’s not valid haha.

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 24, 2008, 8:44am UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667/4 "2008-04-24T08:44:53Z")

</div>

Bilyk, Alex wrote:

> I believe this is not valid XML. You’d need to covert single ’ to "  
> before you can use XML parsers to look at your format as XML.

Not true at all. Attributes may use single or double quotes; you need  
to avoid the same quote character within the attribute value itself, or  
else use the correct entity instead of the literal.

REXML has a habit of slurping in XML, creating a node tree, and then  
using it’s own desired set of quote marks for attributes when emitting  
text, even if that is not what you passed in as the raw text.

It also seems to do some unfortunate entity replacement, creating  
non-XML despite what it may have been given.

> Post your  
> XML file some place like [http://www.stg.brown.edu/service/xmlvalid/](http://www.stg.brown.edu/service/xmlvalid/) and  
> see if it’s valid.

Valid and well-formed are two different things. The issue seems to be a  
matter of well-formed XML. That URL seems to be looking to validate XML  
against some DTD.

An easy way to check some XML is to load it into Firefox or Internet  
Explorer. They should yell if the markup is malformed.

Or use tidy.

–  
James B.

[www.rubyaz.org](http://www.rubyaz.org) - Hacking in the Desert  
[www.risingtidesoftware.com](http://www.risingtidesoftware.com) - Wicked Cool Coding  
[www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 24, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667/5 "2008-04-24T23:12:12Z")

</div>

On 24.04.2008 02:17, Stedwick wrote:

> I have this line in my XML file that was created by REXML
> 
> And I simply cannot access that element using REXML, at all, in any  
> way, no matter what, as far as I can tell…

I am not sure what you mean, it works for me:

irb(main):003:0\> s=""  
=\> “”  
irb(main):004:0\> d = REXML::Document.new s  
=\> … \</\>  
irb(main):005:0\> d.elements  
=\> #\<REXML::Elements:0x7fed20f4 @element= … \</\>\>  
irb(main):006:0\> d.elements.each {|e| p e}  
  
=\> []  
irb(main):007:0\> d.elements.each {|e| p e.attributes}  
{“name”=\>name=‘03x08 - Future’s End ~ 1’, “rating”=\>rating=‘100’}  
=\> []  
irb(main):008:0\> d.elements.each {|e| p e.attributes[“name”]}  
“03x08 - Future’s End ~ 1”  
=\> []  
irb(main):009:0\> d.elements.each {|e| p e.attributes[“rating”]}  
“100”  
=\> []

> I have tried escaping the apostrophes, I have tried converting them to  
> ', I have tried both directly accessing the element and using  
> XPath, nothing works.

As I said, at least direct access works for me.

> Does anybody have any tricks for how to access elements that have  
> attributes with apostrophes in their names? Thanks!

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 24, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667/6 "2008-04-24T17:42:56Z")

</div>

On Wed, 23 Apr 2008 17:17:32 -0700, Stedwick wrote:

> Does anybody have any tricks for how to access elements that have  
> attributes with apostrophes in their names? Thanks!

This has been discussed at  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/117253](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/117253)

When will REXML support parameterized XPath queries (the way _all_ SQL  
databases support parameterized SQL queries)?

–Ken

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [April 25, 2008, 12:41am UTC](https://www.ruby-forum.com/t/rexml-cannot-handle-apostrophes/135667/7 "2008-04-25T00:41:20Z")

</div>

On Thu, 24 Apr 2008 10:34:58 -0500, Ken B. wrote:

> > ', I have tried both directly accessing the element and using  
> > databases support parameterized SQL queries)?
> 
> –Ken

I spoke too soon. I found it, thought it’s just a little short of being  
documented. (Somehow, when generating Ruby 1.8 docs for [ruby-doc.org](http://ruby-doc.org),  
RDoc didn’t extract the variables parameter.)

Consider the following:

doc=REXML::Document.new open(“appraisal.xml”)  
node=REXML::XPath.first doc, ‘//lexeme[phrase/text()=$name]’,  
{}, {“name”=\>“n’t”}  
puts node

 n't 

However, this doesn’t work with  
doc.elements[‘//lexeme[phrase/text()=$name]’,{“name”=\>“n’t”}]  
which returns nil.
