Chapter 64. Escaping XML strings

#include <x/xml/escape.H>

std::string str=x::xml::escapestr("<URL:http://www.example.com>");

x::xml::escapestr() is a miscellaneous utility function replaces all occurences of the characters <, >, and & with &lt;, &gt;, and &amp;.

If the optional second parameter is specified as true, the ', ", and control characters (U+0x0000 to U+0x001F) get replaced with &#xN;, where N is the hexadecimal value of the replaced character.

A more generic alternative is the x::xml::escape() template function. This function defines a string using a beginning and an ending iterator. The replacement string is written to an output iterator. x::xml::escapestr(str, flag) is exactly equivalent to:

std::string s;

x::xml::escape(str.begin(), str.end(),
               std::back_insert_iterator<std::string>{s}, flag);

return s;

The optional flag carries the same meaning as the optional parameter to x::xml::escape(). Furthermore, the beginning and the ending input iterators may iterate over integer values larger than a char or unsigned char. If so, integer values 0x80 and higher get also replaced by &#xN;.

std::string str;

std::string xpathq=x::xml::xpathescapestr(str);

x::xml::xpathescapestr() takes a literal string and produces an XPath expression that evaluates to the string. Literal strings in an XPath expression use quoted or apostrophes, but XPath does not have an escaping mechanism for quotes or apostrophes. x::xml::xpathescapestr() effectively produces 'str', and any literal apostrophes in the string get replaced by an expression that evaluates to an apostrophe.

std::string s;

x::xml::xpathescape(str.begin(), str.end(),
               std::back_insert_iterator<std::string>{s});

return s;

A more generic alternative is the x::xml::xpathescape() template function. This function produces an expression for a literal string that's defined by a sequence, with a beginning and an ending iterator. The XPath expression gets written to an output iterator.

Note

Null characters in the literal strings cannot be handled properly, and will result in a broken XPath expression.