[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

REXML cannot handle apostrophes?

Stedwick

4/24/2008 12:18:00 AM

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!
6 Answers

Bilyk, Alex

4/24/2008 12:34:00 AM

0

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

-ab

-----Original Message-----
From: Stedwick [mailto:philip.brocoum@gmail.com]=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!


Stedwick

4/24/2008 12:38:00 AM

0

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.

James Britt

4/24/2008 6:44:00 AM

0

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

Ken Bloom

4/24/2008 3:35:00 PM

0

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

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


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

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Robert Klemme

4/24/2008 9:10:00 PM

0

On 24.04.2008 02:17, Stedwick wrote:
> 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"
=> [<episode name='03x08 - Future&apos;s End ~ 1' rating='100'/>]

> 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

Ken Bloom

4/24/2008 10:38:00 PM

0

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

> On Wed, 23 Apr 2008 17:17:32 -0700, Stedwick wrote:
>
>> 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-t...
>
>
> 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.

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...