Forum: Ruby Double Quotes in XML

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F9497029243610884623d758ec1c8984?d=identicon&s=25 gparsons (Guest)
on 2006-03-17 21:35
(Received via mailing list)
I'm trying to do something similar to the following:

<?xml version="1.0" encoding="utf-8"?>
<Rules id="ruleset1">
  <Rule num="1">
    <Description>Foo must equal "bar".</Description>
    <Code>Foo = "bar"</Code>
  </Rule>
</Rules>

I'm reading the actual rules and descriptions in from a giant file and
when i spit it back out into an xml file i get errors involving the
quotes around bar.  Looking at examples that should be working i notice
there's a difference between the quotes (if this will show up in groups
it's: " vs " ).  I'm by no means an xml expert (or even novice) so i
may just be missing something here, but it seems like it's something to
do with the encodings maybe?  Anyone have any suggestions?

- Geoff
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-03-17 21:50
(Received via mailing list)
On 3/17/06, gparsons <geoffrey.parsons@gmail.com> wrote:
> I'm reading the actual rules and descriptions in from a giant file and
> when i spit it back out into an xml file i get errors involving the
> quotes around bar.  Looking at examples that should be working i notice
> there's a difference between the quotes (if this will show up in groups
> it's: " vs " ).  I'm by no means an xml expert (or even novice) so i
> may just be missing something here, but it seems like it's something to
> do with the encodings maybe?  Anyone have any suggestions?

You need to do one of the following.
     <Code Foo="bar"/>
     <Code><Foo>bar</Foo></Code>
     <Code>Foo=&quot;bar&quot;</Code>

I prefer the first option listed above.

On another note, I don't think it's typical to use uppercase the way you
are.
I'd do it like this.
      <code foo="bar"/>
F9497029243610884623d758ec1c8984?d=identicon&s=25 gparsons (Guest)
on 2006-03-17 22:30
(Received via mailing list)
Unfortunately i don't have much control over the markup (i'm not even
sure the program that is reading this in is expecting valid xml...
sigh).  turns out it was a problem with what i was reading in having
some funky stuff going on.
E3c79c779c0b390049289cdfe7cb9705?d=identicon&s=25 Bob Hutchison (Guest)
on 2006-03-18 01:11
(Received via mailing list)
On Mar 17, 2006, at 3:33 PM, gparsons wrote:

> I'm trying to do something similar to the following:
>
> <?xml version="1.0" encoding="utf-8"?>
> <Rules id="ruleset1">
>   <Rule num="1">
>     <Description>Foo must equal "bar".</Description>
>     <Code>Foo = "bar"</Code>
>   </Rule>
> </Rules>

This is well-formed XML.

(BTW, there is nothing wrong with the use of caps in the element
names, maybe slightly unusual but that's all -- just don't forget
that XML is case sensitive)

> do with the encodings maybe?  Anyone have any suggestions?
What is giving you the errors? If I read you correctly, it is when
you are writing the XML, so it isn't a parser giving you trouble.

Cheers,
Bob

>
> - Geoff
>
>

----
Bob Hutchison                  -- blogs at <http://www.recursive.ca/
hutch/>
Recursive Design Inc.          -- <http://www.recursive.ca/>
Raconteur                      -- <http://www.raconteur.info/>
xampl for Ruby                 -- <http://rubyforge.org/projects/xampl/>
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-03-18 01:23
(Received via mailing list)
On 3/17/06, Bob Hutchison <hutch@recursive.ca> wrote:
> >   </Rule>
> > </Rules>
>
> This is well-formed XML.

I think it's not well-formed because the Code element contains text
with unescaped double-quotes.  If you change them to &quot; then it's
well-formed.
E3c79c779c0b390049289cdfe7cb9705?d=identicon&s=25 Bob Hutchison (Guest)
on 2006-03-18 12:00
(Received via mailing list)
On Mar 17, 2006, at 7:22 PM, Mark Volkmann wrote:

>>>     <Code>Foo = "bar"</Code>
>>>   </Rule>
>>> </Rules>
>>
>> This is well-formed XML.
>
> I think it's not well-formed because the Code element contains text
> with unescaped double-quotes.  If you change them to &quot; then it's
> well-formed.

It isn't really a matter of opinion, and you are nowhere near lazy
enough :-) Run it through a validator. Both rxp and xmllint believe
that it is OK. That is not to say that it wouldn't be better to
escape those three characters ( quot, apos, and gt --  you *must*
escape lt and amp) -- there's a lot of buggy software out there, and
a little prevention can save you lots of trouble.
.

Cheers,
Bob

>
> --
> R. Mark Volkmann
> Object Computing, Inc.
>

----
Bob Hutchison                  -- blogs at <http://www.recursive.ca/
hutch/>
Recursive Design Inc.          -- <http://www.recursive.ca/>
Raconteur                      -- <http://www.raconteur.info/>
xampl for Ruby                 -- <http://rubyforge.org/projects/xampl/>
This topic is locked and can not be replied to.