Updated April 6, 2023
Definition of XML Special Characters
Special Characters, also named a non-Latin character in XML, are assigned inside the XML file with the numeric Character reference by replacing entities. These characters are appeared in the escaped format using entity formation. The special Characters <, > are converted into escaped equivalent like < and >. Few developers find it difficult to use XML special characters; let’s say, when embedding a few texts inside a document with an ampersand, the parser couldn’t process the document. So, under CDATA sections, a Special Characters encoding should use XmlFormat () while this function handles Special characters for effective changes.
Syntax
There is no Specific Syntax to declare. It could be assigned in an attribute or an element like:
<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance”>
<Latinchar>͍ </ Latinchar >
</Report>
XML special characters with an example
Certain Characters are treated special when progressing XML Document, and these are used as Mark-up Syntax should be escaped approximately. These characters cannot be inserted directly as an attempt to the XML file so that there need to be a character reference or a numeric reference to preserve across the working platforms.
< | Left bracket |
> | Right Bracket |
& | Ampersand symbol |
" | Double Quotes |
&apos | Single Quotes |
For instance, a file containing XML has
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<item_list>
Product size > applied
</item_list>
Now the result of Processing the above sample is given as
Element: <item_list>
Char: Product size > applied
End element: </item_list>
When an XML file is big, including Special Characters in such a case, we can use the CDATA section to handle it. Another section of XML is Xpath which uses Special Characters in their Path Language.
. | Here the current context should be Specified in the node. |
* | This character is named a wild Character use to select all the elements. |
@ | Specifically selects an attribute. |
() | In an Xpath expression, this specifies a grouping. |
: | This is a Namespace Separator |
These could be used in a variety of ways, and it is a good start to learn how to use these into numeric formats in a variety of applications and in XML.
In the following section, let’s see how to use special entities in language with XML construction. Let’s get started here.
Example #1 – Using Cdata
res.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Special Characters in Math Expressions functions. </title>
</head>
<body>
<h1><b> This example Shows how to use Special characters in a expression or paragraph using XML </b></h1>
<div id ="demo1">
<! [CDATA[
Lets take an expression of a formula " if Q(a,c) is a c < Q && a > A or c > a "
]]>
</div>
</body>
</html>
Output:
Example #2 – Using apos and <
Let’s talk about character data in the XML document, and here ‘&’ and “< “should not be used in the mark-up, but apos and quotes could be appearing directly without encoding. For instance, code like
<?XML version="1.0”?>
<password > “ ‘ > </password>
The above sample code is valid; therefore, the XML parser parses and validates the result as
Output:
Example #3 – Using Complete Special Characters
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book ISBN="586954">
<title>"Java Advanced "</title>
<author>Emey mark</author>
<category>Programming & Analysis</category>
<edition>1</edition>
<price> <29.99 ></price>
</book>
<book ISBN="56858">
<title>"More for Python "</title>
<author>&Hebrew manni</author>
<category>Programming</category>
<price>15.99</price>
</book>
<book ISBN="66547">
<title>"The Complete Guide to Animals "</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
In the above code, an XML element is assigned with the special character tags.
Output:
Example #4 – Using the ampersand
<?xml version="1.0" encoding="UTF-8"?>
<password > & </password>
<Login> &@</Login>
Output:
Example #5 – With ampersand and Numeric
<?xml version="1.0" encoding="utf-8"?>
<Result>
<College noOfWorkers="-4120">
<Principal>
<LName>&DivyaSri</LName>
<Idno>124</Idno>
</Principal>
<Faculty>
<FName>&Anish</FName>
<LName>Ram</LName>
<FfName>nagesh</FfName>
<Idno>1235</Idno>
</Faculty>
<Dept>string</Dept>
</College>
</Result>
Explanation
In the above code, the Fname element is declared with a special character, and their presence is reflected in the output as shown below:
Output:
Example #6 – Using > with CDATA structure
<?xml version="1.0" encoding="UTF-8"?>
<return_val>]]></return_val>
Output:
Example #7 – Using wildcard characters in xml
ord.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 = "/">
<html>
<body>
<h2> <center>Restaurant-Online </center></h2>
<xsl:apply-templates select = "Ordering/*" />
</body>
</html>
</xsl:template>
<xsl:template match = "Ordering/*">
<xsl:apply-templates select = "@login" />
<xsl:apply-templates select = "Ordering Menu" />
<xsl:apply-templates select = "Food Pricing" />
<xsl:apply-templates select = "Services" />
<xsl:apply-templates select = "Duration delivery" />
<br />
</xsl:template>
<xsl:template match = "@login">
<span style = "font-size = 22px;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "Ordering Menu">
Lang:<span style = "color:blue;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "Food Pricing">
Tutor:<span style = "color:orange;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "Service">
Experience:<span style = "color:green;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "Duration delivery">
Duration:<span style = "color:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>
ord.xml
<?xml version="1.0" encoding="UTF-8"?>
<Ordering>
<order>
<OrderingMenu>Pizza</OrderingMenu>
<DurationDelivery>15minutes</DurationDelivery>
<FoodPricing>fiftypound</FoodPricing>
<Services>24hours</Services>
</order>
<order>
<OrderingMenu>Burger</OrderingMenu>
<DurationDelivery>10minutes</DurationDelivery>
<FoodPricing>20pounds</FoodPricing>
<Services>12hours</Services>
</order>
<order>
<OrderingMenu>Pizzacheese</OrderingMenu>
<DurationDelivery>20minutes</DurationDelivery>
<FoodPricing>25Pounds</FoodPricing>
<Services>10hours</Services>
</order>
</Ordering>
Explanation
In the above code, the root node is selected by the wildcard (*), in which the child elements and the attribute are selected.
Output:
Explanation
In the above wildcard example, we have used an XML document with XSL, and a particular value and attributes are selected.
Conclusion
This article demonstrates how to use these Special Characters in XML in various Circumstances. In the case of Xpath, the result produced by the expression is in the form of Node-set. The overall concept is while using Special Characters in XML file; care should be done on the side of the Client as we need to escape a Character. Also, we have gone through a detailed description with examples of these special characters.
Recommended Articles
This is a guide to the XML Special Characters. Here we discuss how to use XML Special Characters along with the examples and outputs. You may also have a look at the following articles to learn more –