EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials XML Tutorial XML ampersand
 

XML ampersand

Priya Pedamkar
Article byPriya Pedamkar

Updated April 11, 2023

XML ampersand

 

 

Definition of XML ampersand

XML Ampersand is defined as a Special character and we must escape it within an XML document without causing Parsing error. They could occur inside a CDATA section and in the comments section in unescaped format. They have a special meaning in a mark-up language such that they are introduced along as entity references like less than, quote, single quote. When &amp is used in XML it shows as &amp whereas in HTML it is rendered as ‘&’. According to W3C Recommendations, a few entities are replaced ad other delimiters are replaced in an XML Document.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

The general Syntax is defined as

enter &paste;
In API call it is used as %26amp;

To use the ampersand in XML document it could be assigned along with the entity name followed by a semicolon as shown above. This article explains how to specify character entities in XSLT and XML by replacing special entities.

How does ampersand work in XML?

Generally, XML takes the ampersand differently in their document when comparing with other characters. This Character reference is strictly prohibited when assigned inside an attribute value or an element. XML Parser parse the unescaped ampersand in the configuration files that says it’s an entity that needs to be parsed into some other references. When we assign ampersand without any descriptive characters or importantly without semicolon the parser gives an error. Speaking virtually, they are valid and parsing was integral to any new programming languages. In case of ampersands in XML the starting character in the escape sequence is another ampersand itself called an escaping mechanism. Our XML Library has a certain standard for creating SAX Applications. Using this API, we can handle this unescaped characters. They are escaped in either element content or attribute content. For example, Let’s take an XML code

from xml.sax.saxutils import escape, unescape
myx = "<ele>Hickory & Dickory &Dock; Spot</ele1>"
myx= unescape(myx)
myx= escape(myx)
print(myx)

Lets see an another sample

<?xml version="1.0" encoding="UTF-8"?>
<book>
<body> Q&ecirc;Rs &amp; ghf&ccedil;&xtilde;</body>
</book>

When you validate this XML code it throws an error in line 3 stating entity ‘&ecirc’ not defined.

To handle the parser on this it has built-in XMLHttpRequest and this returns a String as a value.

In android String XML ampersand (&) Character is implemented like HTML encode not directly with XML Syntax as it violates a validation. To insert a text with ampersand it is done in the following way:

<string name="my demo">This is my sample test with &amp; ampersand</string>

In other cases, like XML-to-XML transformations we need to prefer special characters like entities. If we just do in XSL wherever we want ‘&’ in XML as shown below. In XSLT, we cannot use ampersand in between tags or attribute. The transformation engine throws error when these characters are used incorrectly.

<xsl:text>&amp;#</xsl:text>
<xsl:value-of select="$empID" />
<xsl:text>;</xsl:text>

This gives XML Output as

'

Let’s take other scenario with DOCTYPE StyleSheet where the special characters for native language is demonstrated using ‘&’.

<!DOCTYPE stylesheet [
<!ENTITY new
"<xsl:text disable-output-escaping='yes'>&amp;new;</xsl:text>">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output doctype-system="samplet.dtd"/>
<xsl:template match="exam">
<example>
The native word for "Norgen" is "Espa&ntilde;a".
<xsl:apply-templates/>
</example>
</xsl:template>
</xsl:stylesheet>

A template test is shown as like this to handle Apostrophe and Ampersand in URL.

<xsl:template name="formStructure">
<xsl:param name="text" />
<xsl:variable name="new">&amp;#39;</xsl:variable>
<xsl:variable name="new2">&amp;amp;</xsl:variable>
<xsl:variable name="new3">%27</xsl:variable>
<xsl:variable name="new4">%26</xsl:variable>
<xsl:choose>
<xsl:when test="contains($text, $apos)">
<xsl:variable name="before" select="substring-before($text,$apos)">
</xsl:variable>
<xsl:variable name="after" select="substring-after($text,$apos)">
</xsl:variable>
<xsl:call-template name="formStructure">
<xsl:with-param name="text" select="$before" />
</xsl:call-template>
<xsl:value-of select="$new3" />
<xsl:call-template name="formStructure">
<xsl:with-param name="text" select="$after" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($text, $amp)">
<xsl:variable name="before" select="substring-before($text,$amp)">
</xsl:variable>
<xsl:variable name="after" select="substring-after($text,$amp)">
</xsl:variable>

In the next section we shall see few demo on XML Ampersand using XML code and XSLT Sheets. So lets get Started.

Examples

Let us discuss examples of XML ampersand.

Example #1: Simple XML Code with Ampersand

<?xml version="1.0"?>
<content attribute="An ampersand over in XML is escaped as &amp;">
An ampersand a character reference can also be escaped as &amp; in element content of XML.
</content>

Explanation

Here we have created an attribute with a Character entity. When we execute the above code, we get the output as

Output:

XML ampersand 1

Example #2

<?xml version="1.0"?>
<itemlist>
<INo>2545</INo>
<IName>Hotel &amp; placed well in California</IName>
<vId>15452hjg9</vId>
<Type>restaurant</Type>
</itemlist>

Explanation

Again this is a simple example with XML Element without Attribute values.

Output:

XML ampersand 2

Example #3 : Using XSLT XML stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="lang.css" type="text/css"?>
<lang xml:lang="ta">
<LName>
<norskname>Harry &amp; Tom</norskname>
<Hetername>Büulrr</Hetername>
<Location>♀</Location></LName>
<LName>
<norskname>John &amp; Sovern</norskname>
<Hetername>Büffer</Hetername>
<Location>♂</Location>
</LName>
</lang>

Explanation

In the above code, special characters are assigned along with an &amp.

Output:

XML ampersand 3

Example #4

<?xml version="1.0" encoding="utf-8"?>
<Testing>
<Employee noOfWorkers="5420">
<TeamLeader>
<EName>&amp;JaneSri</EName>
<EIdno>224</EIdno>
</TeamLeader>
<Manager>
<MName>&Anand</MName>
<LName>Rakes</LName>
<Project>PHP</Project>
<MIdno>545</MIdno>
</Manager>
<Dept>Computers</Dept>
</Employee>
</Testing>

Output:

XML ampersand 4

Example #5

co.xml

<?xml version="1.0" encoding="UTF-8"?>
<Western>
<Europe>
<EName>Poland</EName>
<Capital>Warsaw t</Capital>
<SubRegion>Eastern</SubRegion>
<company>Microsoft</company>
<Currency>Polish</Currency>
</Europe>
<Europe>
<EName>Spain</EName>
<Capital>Madrid S</Capital>
<SubRegion>Western</SubRegion>
<company>Amazon</company>
<Currency>Euro</Currency>
</Europe>
<Europe>
<EName>Italy</EName>
<Capital>Rome</Capital>
<SubRegion>Southern</SubRegion>
<company>Verizon</company>
<Currency>Euro</Currency>
</Europe>
<Europe>
<EName>Sweden</EName>
<Capital>Stochkolm</Capital>
<SubRegion>Eastern</SubRegion>
<company>TCS</company>
<Currency>Krona</Currency>
</Europe>
<Europe>
<EName>Netherland</EName>
<Capital>Amsterdam</Capital>
<SubRegion>Western</SubRegion>
<company>Columbia</company>
<Currency>Euro</Currency>
</Europe>
<Europe>
<EName>Norway</EName>
<Capital>Oslo</Capital>
<SubRegion>Western</SubRegion>
<company>Crayon</company>
<Currency>krone</Currency>
</Europe>
<Europe>
<EName>Sweden</EName>
<Capital>Bob Dylan</Capital>
<SubRegion>USA</SubRegion>
<company>Columbia</company>
<Currency>10.90</Currency>
</Europe>
</Western>

co.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"
<xsl:value-of select="translate(EName, '&amp;', '')" />
<html>
<body>
<h1>European Countries:</h1>
<table border="2">
<tr bgcolor="brown">
<th>EName</th>
<th>Capital</th>
</tr>
<tr>
<td><xsl:value-of select="translate(EName, '&amp;', '')" /></td>
<td><xsl:value-of select="translate(EName, '&amp;', '')" /></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Explanation:

Two files are created here one for XML and XSL the value is selected and assigned Ampersand to it. So when we execute the above snippet we get the output as

Output:

Example 5

Example #6: Using ‘&’

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book ISBN="586954">
<title>&quot;Java Advanced &quot;</title>
<author>Emey mark</author>
<category>Programming & Analysis</category>
<edition>1</edition>
<price> &lt;29.99 &gt;</price>
</book>
<book ISBN="56858">
<title>&quot;More for Python &quot;</title>
<author>&amp;Hebrew manni</author>
<category>Programming</category>
<price>15.99</price>
</book>
<book ISBN="66547">
<title>&quot;The Complete Guide to Animals &quot;</title>
<author>joe emacul</author>
<author>sheeba david</author>
<author>Jacob ethen</author>
<category>Wild</category>
<category>Teresstial</category>
<language>French</language>
<edition>5</edition>
<price>31.99</price>
</book>
</bookstore>

Explanation

The above code uses simple ‘&’ ampersand along with the Special Characters.

Output:

Example 6

Conclusion

To conclude, this topic demonstrates how to use these entity characters in various scenarios also we have seen how it is invalid in a few XML languages. Many of the organizations use an ampersand in their application on product names and references by the developers. The threat of them is viewed differently to manage them it is been assigned using entity mark-ups. This may highlight a red flag when parsing it.

Recommended Articles

This is a guide to XML ampersand. Here we discuss the Definition, How does ampersand work in XML?, and examples with code implementation. You can also go through our other suggested articles to learn more –

  1. XML Mapping
  2. XML HttpRequest 
  3. XML Element
  4. XML HttpRequest 
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW