Updated April 6, 2023
Introduction to PCDATA in XML
XML Parsed Character Data (PCDATA) is defined as a parser reads a text in the XML document, means it parses every character and determines the exact meanings. It’s a data definition that evolved from Standardized Generalized Mark-up Language to specify mixed content XML elements. The element content of DTD is defined as PCDATA which parse the content of the element which is a plain text and gives the result as mark-up. The Parsed Character data implies non-mark-up text and unable to differentiate between the data types like characters, int, float etc.
Syntax
The common syntax of PCDATA is declared with the structure of DTD which is given below: The content model is
<!ELEMENT element-name (#PCDATA)>
Example:
- <!ELEMENT balls (#PCDATA)>: Here a Balls element is considered to be some part of XML document. And the sample Xml of above DTD declaration line is given as
- <balls> football </balls>: The string “football” would be defined as Parsed Character data
The above statement implies that the element balls contain data that is going to be parsed by the parser. PCDATA prefers to allow alphanumeric data.
How PCDATA works in XML?
Before we start with PCDATA it is necessary to learn the DTD concept of XML as PCDATA is declared herewith. The main benefit of DTD is to describe the structure of an XML file which has a set of elements, attributes, entities in it. The term entity references play a vital role in PCDATA. Some characters like (<, >,&, “, ‘ ) which are specified in XML syntax should be replaced by so-called forms called entity references to avoid mark-up tags confusion errors.PCDATA navigates through all the data’s in the XML documents.
For example, let us take a simple example and would see how the parser analyses each of the characters and mark-up tags.
Example #1
Below are some sample code:
Code #1
<analogies>
It makes students analyze the vocabularies and understands the relationships
between the mathematical terms. For example > 5 and <= 5.
</analogies>
The above Statement causes an error, as the parser reads the line 3 with (> 5) it assumes to be a beginning of an element as >, <, / these characters have a unique meaning in the mark-up terms. Hence, to include this it is better to use equivalent entities like < >. Care Must be taken while declaring Document elements in DTD as an element declaration imposing #PCDATA value is not provided for child elements. Next, PCDATA goes well with mixed content in DTD.
The sample usage is given below:
Code #2
<!ELEMENT p (#PCDATA|m|n)*>
Sample XML is given:
<p>Paragraph data <m/> with limited commas <m/> and more text <n/></p>
Choosing PCDATA and an attribute in a DTD is to maintain an efficient data field in the document. In the earlier articles, we have seen different components of DTD and how they are declared with both inline and external files. Let’s see a brief sample with an Element tag associating PCDATA.
Code #3
<?xml version="1.0"?>
<Furniture>
<Sofas>soft</Sofas>
<Recliners> high</Recliners>
<Gliders>cushion</Gliders>
<Chairs>medium height</Chairs>
<Table>Accounting</Table>
</Furniture>
DTD
<!ELEMENT Furniture (Sofas, Recliners, Gliders, Chairs, Table)>
<!ELEMENT Sofas (#PCDATA)>
<!ELEMENT Recliners (#PCDATA)>
<!ELEMENT Gliders (#PCDATA)>
<!ELEMENT Chairs (#PCDATA)>
<!ELEMENT Table (#PCDATA)>
]>
The XML file above includes an element Furniture and this element has the following five child elements each hold text data. And the DTD below is used to validate the XML with parsers. Now, we are ready to declare the Furniture element which must contain (Sofas, Recliners, Gliders, chairs, Dressers). The declaration of all the child elements is very simple as it contains only one text. Therefore, all the remaining elements have #PCDATA models.
Code #4
<!ELEMENT Furniture (Sofas, Recliners, Gliders, Chairs, Dressers)>
<!ELEMENT Sofas (#PCDATA)>
<!ELEMENT Recliners(#PCDATA)>
<!ELEMENT Gliders (#PCDATA)>
<!ELEMENT Chairs(#PCDATA)>
<!ELEMENT Table(#PCDATA)>
Example #2
Below is the sample code mentioned:
Code
<!ELEMENT certificate_details (sur_name,first_name+,residence)>
<!ELEMENT sur_name (#PCDATA)>
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT residence
(area,(city|district),(state|pincode),>
<!ELEMENT area (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT district (#PCDATA)>
<!ELEMENT state (#PCDATA)>
Examples to Implement PCDATA in XML
In this section, we shall see some examples by creating a simple DTD by defining some elements and entities which is useful for further identification purpose.
Example #1
Simple Example showing Internal DTD in XML with element text declared as PCDATA.
Code: air.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<!DOCTYPE Airline [
<!ELEMENT Airline (aname, country, class)>
<!ELEMENT aname (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT class (#PCDATA)>
]>
<Airline>
<aname>Alitalia</aname>
<country>Italy</country>
<class>passengers- business</class>
</Airline>
The DTD is a schema for XML document says that the element names text is Parsed Character data.
Output:
Example #2
PC-DATA works with an element declaring more children
Code: air.xml
<?xml version="1.0"?>
<!DOCTYPE restaurant [
<!ELEMENT restaurant (dishes*)>
<!ELEMENT dishes (#PCDATA)>
]>
<restaurant>
<dishes> pizza</dishes>
<dishes>Pasta</dishes>
<dishes>Do-Nuts</dishes>
</restaurant>
Here we have one element restaurant which is followed by a child element dishes and its text is declared as PCDATA to have different items on the dishes list.
Output:
Example #3
Implementing PCDATA in external DTD.
Code: addr.dtd
<!ELEMENT residence (aname,company,fax)>
<!ELEMENT aname (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT fax (#PCDATA)>
address.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<!DOCTYPE address SYSTEM "addr.dtd">
<residence>
<aname>Divya Mary</aname>
<company>EDUCBA</company>
<fax>(022) 013-5621</fax>
</residence>
Output:
Example #4
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE grocerystore [
<!ELEMENT grocerystore (grocery+)>
<!ELEMENT grocery (Name, type+, category*, country?, mfd?, expdate?, price)>
<!ATTLIST grocery prodcode CDATA #REQUIRED>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT type (#PCDATA)>
<!ELEMENT category (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT mfd (#PCDATA)>
<!ELEMENT expdate (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<grocerystore>
<grocery prodcode="52486112">
<Name>Gerbers puffs</Name>
<type>Cereal</type>
<category>Children</category>
<mfd>2008</mfd>
<expdate>2009</expdate>
<price>11.79</price>
</grocery>
<grocery prodcode="65824575">
<Name>Breakfast French Toast</Name>
<type>Cereals</type>
<category>Adults</category>
<mfd>2007</mfd>
<price>28.99</price>
</grocery>
<grocery prodcode="65214735">
<Name>Moong dhal</Name>
<type>All</type>
<type>Adults</type>
<type> Indian types </type>
<category>Indian Lentils</category>
<category>Icecream</category>
<country>USA</country>
<mfd>2010</mfd>
<expdate>2012</expdate>
<price>54.99</price>
</grocery>
</grocerystore>
Output:.
Conclusion
In very general terms, an efficient way to represent the text in DTD is by #PCDATA. Therefore, in this article we have introduced PCDATA with a small introduction, also we have done with the working principles in DTD with few implementations. And most of the features of XML language is pure with the attributes and elements which is defined here. With this enough background, you can create an application between a client and a server.
Recommended Articles
This is a guide to PCDATA in XML. Here we discuss an introduction to PCDATA in XML with appropriate syntax, working and respective examples. You can also go through our other related articles to learn more –