Escaping pango makut character in XML

I’m in a very curious situation. I have a Gtk.Label created as shown below with markup support enabled:


                <child>
                  <object class="GtkLabel" id="_pr_label">
                    <property name="label" translatable="yes">&lt;b&gt;Functions parameters:&lt;/b&gt;.

&lt;b&gt;Ex.:&lt;/b&gt; &gt;15: greater than number(inclusive).
&lt;b&gt;Ex.:&lt;/b&gt; 6,8,10; relation of arbiterary numbers.
&lt;b&gt;Ex.:&lt;/b&gt; 4; a singles number.
&lt;b&gt;Ex.:&lt;/b&gt; &lt;10: lower than number (inclusive).
&lt;b&gt;Ex.:&lt;/b&gt; 80..90; Interval between numbers (inclusive).
                    </property>
                    <property name="wrap">true</property>
                    <property name="justify">3</property>
                    <property name="xalign">0</property>
                    <property name="use-markup">true</property>
                    <property name="margin-top">15</property>
                  </object>
                </child>

The XML escapes the “<” character using “<”, but the pango markup generates an error because I don’t know how to escape the character when the text is created directly in the XML (If it is created in the code there are ways to do it).

from markup due to error parsing markup: ...: “10” is not a valid character following a “<” character; it may not begin an element name

Is there a way to escape the “<” character for the pango markup without using a function for this? Directly via some element in the text?

The funny thing is that the “>” character doesn’t cause any problems.

It’s simply XML inside XML, so escape twice: &amp;lt;10: lower than.
Let’s walk it:

  • you mean literal <10: lower than
  • but it’s gonna be parsed as Pango markup, so you need to escape the <, which gives &lt;10: lower than
  • but this value itself is in the GtkBuilder XML markup, so you need to escape it once more, and here it means the &: &amp;lt;10: lower than.

And even though > might not cause as many problems, you should still escape any &, < and >s to be safe and valid.

HTH

1 Like