REXML cannot handle apostrophes?

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!

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/ and
see if it’s valid.

-ab

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.

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/ 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 - Hacking in the Desert
www.risingtidesoftware.com - Wicked Cool Coding
www.jamesbritt.com - Playing with Better Toys

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

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

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

–Ken

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,
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.