If an element will contain a mix of text and additional child elements, it is said to have mixed content.
The following series of images displays an example of working with mixed content.
Question: What is the purpose of #PCDATA in XML?
In XML, #PCDATA is a keyword used to indicate that an element may contain character data (i.e. plain text) as its content. The "#PCDATA" notation stands for "Parsed Character Data" and is used to define elements that contain text content that can be parsed and processed by XML parsers.
For example, consider an XML element named "title" that contains only text content:
<!ELEMENT title (#PCDATA)>
In this example, the "title" element is defined using the "#PCDATA" keyword to indicate that it can contain only character data as its content.
Here is an example of an XML document that uses the "title" element:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>XML for Beginners</title>
<author>John Smith</author>
<publisher>Publishing Company</publisher>
<year>2023</year>
</book>
In this example, the "title" element contains the text "XML for Beginners", which is the title of the book.
The #PCDATA keyword is often used in conjunction with other keywords to define more complex element content models, such as mixed content models that allow both text and child elements within an element. For example:
<!ELEMENT paragraph (#PCDATA | emphasis)*>
<!ELEMENT emphasis (#PCDATA)>
In this example, the "paragraph" element can contain either character data or the "emphasis" element in any order, allowing for mixed content within the element. The "emphasis" element is defined as containing only character data using the #PCDATA keyword.
Note that there are no allowed element contents in this declaration. If there were, it would not be an empty element. Remember that when adding empty elements to your XML document , you must add the /
character before the closing angle bracket. If you have an element with this type declaration:
Mixed content models can also contain elements interspersed within the text. Suppose you wanted to include a description of each contact in your XML document. You could create a new element <description>: <description>Tom is a developer and author for <title>Beginning XML</title>, now in its <detail>5th Edition</detail></description>.
In this example, you have a <description> element. Within the <description> element, the text is interspersed with the elements <title> and <detail>.
The next lesson covers referencing DTD declarations in XML.