Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include an ampersand (&) character in an XML document?

Tags:

validation

xml

How can I put an & symbol in an attribute of an XML tag while keeping the XML valid?

<?xml version="1.0" ?>  <a text="b&c"/> 

When I use W3School's validator, I get the error:

EntityRef: expecting ';'

like image 663
nagy.zsolt.hun Avatar asked Jan 24 '13 11:01

nagy.zsolt.hun


People also ask

How do you use ampersand in a sentence?

When several items are listed together, an ampersand may be used to tie together words or descriptions that would otherwise be less clear. For example, you might write: My favorite breakfasts are donuts, pancakes, biscuits & gravy, and bacon & eggs.

How do you use the & symbol?

& is called an ampersand symbol (pronounced “AM- per-sand”). Essentially, it means “and”. It is used both (a) in the body of the paper as part of a citation and (b) at the end of the paper as part of a reference.

How do you use ampersand in titles?

The ampersand should always be used when it's part of a company name, logo, proper noun, or title. This is one of the few cases where ampersand use isn't up for debate. For example, “M&M's” would never be written “M and M's.”


2 Answers

Use a character reference to represent it: &amp;

See the specification:

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; ", and MUST, for compatibility, be escaped using either " &gt; " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.

like image 86
Quentin Avatar answered Sep 28 '22 04:09

Quentin


You can use these escape sequences :

< (less-than)                   -   &#60; or &lt;  > (greater-than)                -   &#62; or  &gt;  & (ampersand)                   -   &#38;  ' (apostrophe or single quote)  -   &#39;  " (double-quote)                -   &#34; 
like image 43
Shubham Avatar answered Sep 28 '22 04:09

Shubham