Double Quotes in XML

G

gparsons

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
 
M

Mark Volkmann

I'm trying to do something similar to the following:

<?xml version=3D"1.0" encoding=3D"utf-8"?>
<Rules id=3D"ruleset1">
<Rule num=3D"1">
<Description>Foo must equal "bar".</Description>
<Code>Foo =3D "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?

You need to do one of the following.
<Code Foo=3D"bar"/>
<Code><Foo>bar</Foo></Code>
<Code>Foo=3D&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 ar=
e.
I'd do it like this.
<code foo=3D"bar"/>
 
G

gparsons

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

Mark Volkmann

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

Bob Hutchison

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top