String EncodeQuotes and EncodeAttribute Examples

iXperion provides a number of vipers for encoding XML attribute values. A number of characters are not allowed in XML attributes values and the vipers EncodeQuotes and EncodeAttribute ensure those characters are correctly encoded.

EncodeQuotes only encodes single and double quotes, while EncodeAttribute encodes not only single and double quotes, but also encodes are removes entire xml element tags.

EncodeQuotes

The viper string.encodequotes is typically used to encode xml attribute values. Xml attribute values are enclosed by either single (') or double (") quotes. Because of this an XML attribute value cannot contain quotes. Technically it's possible to use single quotes when double quotes are used as enclosure and vice versa, but to be on the safe side it's generally wise to encode both single and double quotes when writing XML attribute values. 

A single quote is encoded as numeric entity ' (Internet Explorer doesn't handle the named entity ' well) and the double quote is encoded as named entity ".

EncodeAttribute

Encodes quotes as described above. XML attribute values cannot contain XML element tags. In order to prevent those tags the < and > are encoded or removed entirely. You can specify your preference with the viper overload string.encodeattribute(data, removeTags). The default removes the tags as you can see in the example.

Which one?

EncodeQuotes or EncodeAttribute? It entirely depends on the input you expect. If you are certain the value does not contain XML element tags, you can use EncodeQuotes. If you are unsure use EncodeAttribute.

Smartsite SXML CopyCode image Copy Code
<se:buffer name="data">
    <b>this text contains ' and " quotes</b>
</se:buffer>

<!-- Encodes single and double quotes -->
{string.encodequotes($data)}

<!-- Encodes quotes and removes the bold opening and closed tags -->
{string.encodeattribute($data)}

<!-- Encodes quotes and LT and GT characters -->
{string.encodeattribute($data, false)}
Example Result CopyCode image Copy Code
<!-- Encodes single and double quotes -->

    <b>this text contains &#39; and &quot; quotes</b>


<!-- Encodes quotes and removes the bold opening and closed tags -->

    this text contains &#39; and &quot; quotes


<!-- Encodes quotes and LT and GT characters -->

    &lt;b&gt;this text contains &#39; and &quot; quotes&lt;/b&gt;
SXML