MCCC CIS 177 - Markup Languages

Create a Valid XML Document

Work with Entities

While you enter data directly into an XML document, you may find it more efficient to store data in multiple files. One of the strengths of XML is the ability to use entities to access external data. An XML document is the most basic entity called a document entity, however, entities can also refer to a text string, a DTD, an element or attribute declaration or an external file containing character or binary data. You declare entities in a DTD. The syntax depends on how the entity is classified. The factors involved in classifying an enitity included 1) the content of the entity; 2) how the entity is construction; and 3) where the definition of the entity is located.

An entity that is part of the XML document's content is called a general entity and are often used as placeholders for text strings that repeat throughout the document or in other documents. An entity that is not part of the document's content is called a parameter entity and are used to store the various declarations found in a DTD.

If an entity is constructed using well-formed XML text, it is a parsed entity. Non-XML entities are referred to as unparsed. Graphic files are common examples of unparsed entities. If the entity can be defined with a text string within the document's DTD, it's an internal entity. If the definition relies on an external file, it's an external entity.

General Parsed Entities

General entities are declared in the DTD as follows:

<!ENTITY entity "value">

Examples without and with XML tags:

<!ENTITY Acme "Acme Products">
<!ENTITY Acme "<Company>Acme Products</Company>">

Once you've declared an entity, you can reference it anywhere in your document as follows:

&entity;

Example

<Title>This is the home page of &Acme;.</Title>

For obvious reasons, you cannot use the & symbol as part of your entity's value.

To declare a general external entity use the following syntax:

<!ENTITY entity SYSTEM "URL">

Example

<!ENTITY headlines SYSTEM "http://domain.com/filename.xml">

USED:

<News>&headlines;</News>

Work with General Parsed Entities

Download lab files.

Open Items.dtd in Notepad and examine the contents.

Edit Orders.xml and add the following code:

<!DOCTYPE Customers SYSTEM "Items.dtd"
[
<!ELEMENT Customers (Customer+)>
<!ELEMENT Customer (Name, Address, Phone, E-mail?, Orders)>
<!ATTLIST Customer CustID ID #REQUIRED>
<!ATTLIST Customer CustType (home | business) #IMPLIED>
<!ELEMENT Name (#PCDATA)>
<!ATTLIST Name Title (Mr. | Mrs. | Ms.) #IMPLIED>
<!ELEMENT Address (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ELEMENT E-mail (#PCDATA)>
<!ELEMENT Orders (Order+)>
<!ELEMENT Order (Order_date, Items)>
<!ATTLIST Order OrderID ID #REQUIRED>
<!ATTLIST Order OrderBy IDREF #REQUIRED>
<!ELEMENT Items (Item+)>
<!ELEMENT Order_date (#PCDATA)>
<!ELEMENT Item (#PCDATA)>
<!ATTLIST Item ItemPrice CDATA #REQUIRED>
<!ATTLIST Item ItemQty CDATA "1">
]>

...

<Items>
<Item ItemPrice="599.95">
&DCT3Z;</Item>
<Item ItemPrice="199.95">
&SM128;</Item>
<Item ItemPrice="29.95" ItemQty="2">
&RCL;</Item>
</Items>

...

<Items>
<Item ItemPrice="59.95">
&BCE4L;</Item>
</Items>

...

<Items>
<Item ItemPrice="59.99">
&WBC500;</Item>
<Item ItemPrice="5.95" ItemQty="2">
&RCA;</Item>
</Items>

...

<Items>
<Item ItemPrice="179.99" ItemQty="3">
&SCL4C;</Item>
</Items>

Save and view the file in the browser.

Parameter and Unparsed Entities

Parameter entities are used to stoe the content of a DTD. To declare an internal parameter entity, use the following syntax:

<!ENTITY % entity "value">

USED

%entity; <!-- can only be placed in the DTD and NOT in the XML content -->

Example

<!ENTITY % books
"<!ELEMENT Book (Title, Author)>
<!ELEMENT Book Pages CDATA #REQUIRED>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>"
>

USED

%books;

Declaring parameter entities in an internal DTD is not very practical because it adds an additional step, but doesn't save any time or maintenance effort. Parameter entities are more useful when used to combine multiple external DTDs into a single XML document

Work with Parameter Entities

Open Notepad and create a new file with the following code:

<!ELEMENT Book (Title, Author)>
<!ATTLIST Book Pages CDATA #REQUIRED>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>

Save the file as Book.dtd

Start a new Notepad document with the following code:

<!ELEMENT Magazine (Name)>
<!ATTLIST Magazine Publisher CDATA #REQUIRED>
<!ELEMENT Name (#PCDATA)>

Save the file as Magazine.dtd

Start a new Notepad document with the following code:


<?xml version="1.0" ?>
<!-- document type declaration follows -->
<!DOCTYPE Publications
[
<!ENTITY % Books SYSTEM "Book.dtd">
<!ENTITY % Mags SYSTEM "Magazine.dtd">
%Books;
%Mags;
]>
<Publications></Publications>

Save the file as Pubs.xml and view it in the browser.