Re: [xml] Question about xmlSetProp

Date view Thread view Subject view Author view

From: Daniel Veillard (Daniel.Veillard@w3.org)
Date: Sun Feb 14 1999 - 12:51:55 EST


  Hi Alexander,

> I've recently discovered a bug in my program (Dia) that uses gnome-xml.
>
> To fix this i need some information about the usage of the function
> xmlSetProp(). Is the string i pass to this function supposed to be
> limited in any way (like the xml-spec says) or is it escaped by
> gnome-xml?

  It is escaped automatically when the parsing succeeds.
I guess I need a bit more explanation, I will use the examples you gave:

> The reason i ask is because it acts a bit strange.
>
> Given:
> xmlSetProp(doc->root, "prop1", "gno<me is great");
> It sets (when writing to a file):
> <EXAMPLE prop1="gno&lt;me is great" prop2="&linux; too" emptyprop="">
>
> That is, the '<' is escaped to &lt;

  Yes, the point here is that while it should have been escaped, it didn't
lead to any parsing error actually, so the correct value has been generated
internally and the proper value is then genreated on save, i.e. escaped.

> On the other hand, given:
> xmlSetProp(doc->root, "prop1", "gno&me is great");
> It exports
> <EXAMPLE prop1="gno" prop2="&linux; too" emptyprop="">
>
> That is, the '&' is not escaped (to &amp; or soo i would guess, i'm no
> xml wizard).

  Here there had been a parsing error. The "gno&me is great" string is
interpreted as "gno" then the beginning of the parsing of an entity reference
(letter &) and the name of the entity is not closed properly (it expects
the letter ';') at the end. At that step the only thing left is
"gno" and that's what it outputs.

> Soo, what kind of behaviour can i expect from xmlSetProp()?

  Basically you should provide escaped values for '<' and '&'
Also you don't have the right to use both ' and " in the same attribute
value.

  If this is really a problem, I can provide an alternate routine
doing the escape for you (something like xmlSetRawProp).

  You must also know that pedantic XML processors can remove extra
spaces between words in an attribute value. For example
 <elem prop1="a b c"/>
can be saved as
 <elem prop1="a b c"/>

 Even if libxml don't do it ...

Daniel

-- 
	    [Yes, I have moved back to France !]
Daniel.Veillard@w3.org | W3C  INRIA Rhone-Alpes  | Today's Bookmarks :
Tel : +33 476 615 257  | 655, avenue de l'Europe | Linux, WWW, rpmfind,
Fax : +33 476 615 207  | 38330 Montbonnot FRANCE | rpm2html, Kaffe,
http://www.w3.org/People/W3Cpeople.html#Veillard | badminton, and Amaya.
----
Message from the list xml@rufus.w3.org
Archived at : http://rufus.w3.org/veillard/XML/messages
to unsubscribe: echo "unsubscribe xml" | mail  majordomo@rufus.w3.org


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Wed Aug 02 2000 - 12:29:25 EDT