REXML cannot handle apostrophes?

S

Stedwick

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

<episode name='03x08 - Future&apos;s End ~ 1' rating='100'>

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
&apos;, 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!
 
B

Bilyk, Alex

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

-----Original Message-----
From: Stedwick [mailto:p[email protected]]=20
Sent: Wednesday, April 23, 2008 5:20 PM
To: ruby-talk ML
Subject: REXML cannot handle apostrophes?

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

<episode name=3D'03x08 - Future&apos;s End ~ 1' rating=3D'100'>

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
&apos;, 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!
 
S

Stedwick

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

James Britt

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 Britt

www.rubyaz.org - Hacking in the Desert
www.risingtidesoftware.com - Wicked Cool Coding
www.jamesbritt.com - Playing with Better Toys
 
K

Ken Bloom

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

<episode name='03x08 - Future&apos;s End ~ 1' rating='100'>

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
&apos;, 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!

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
 
R

Robert Klemme

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

<episode name='03x08 - Future&apos;s End ~ 1' rating='100'>

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="<episode name='03x08 - Future&apos;s End ~ 1'
rating='100'/>"
=> "<episode name='03x08 - Future&apos;s End ~ 1' rating='100'/>"
irb(main):004:0> d = REXML::Document.new s
=> <UNDEFINED> ... </>
irb(main):005:0> d.elements
=> #<REXML::Elements:0x7fed20f4 @element=<UNDEFINED> ... </>>
irb(main):006:0> d.elements.each {|e| p e}
<episode name='03x08 - Future&apos;s End ~ 1' rating='100'/>
=> [<episode name='03x08 - Future&apos;s End ~ 1' rating='100'/>]
irb(main):007:0> d.elements.each {|e| p e.attributes}
{"name"=>name='03x08 - Future&apos;s End ~ 1', "rating"=>rating='100'}
=> [<episode name='03x08 - Future&apos;s End ~ 1' rating='100'/>]
irb(main):008:0> d.elements.each {|e| p e.attributes["name"]}
"03x08 - Future's End ~ 1"
=> [<episode name='03x08 - Future&apos;s End ~ 1' rating='100'/>]
irb(main):009:0> d.elements.each {|e| p e.attributes["rating"]}
"100"
=> [ said:
I have tried escaping the apostrophes, I have tried converting them to
&apos;, 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
 
K

Ken Bloom

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

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

<lexeme>
<phrase>n't</phrase>
<entry domain='appraisal'>
<modify att='orientation' type='flip'/>
<set att='polarity' value='marked'/>
<modify att='force' type='flip'/>
</entry>
</lexeme>

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top