Search code examples
libxml2vala

In Vala, why node->set_content doesn't accept the ampersand charater?


The following code

Xml.Node* node = new Xml.Node (null, "name");
node->set_content("&");

produces the following error:

error : unterminated entity reference

Is this a bug or am I doing something wrong?

Both the greater and less than characters work fine. Also, the add_content method works fine with the ampersand character.


Solution

  • Is this a bug or am I doing something wrong?

    The latter. From the xmlNodeSetContent documentation: "content is supposed to be a piece of XML CDATA, so it allows entity references, but XML special chars need to be escaped first...". You can use GLib.Markup.escape_text to do so.

    Also, the add_content method works fine with the ampersand character.

    From the xmlNodeAddContent documentation: "In contrast to xmlNodeSetContent(), @content is supposed to be raw text, so unescaped XML special chars are allowed, entity references are not supported."