Home arrow XML arrow Page 6 - XML Basics (part 2)

An Entity In The Attic - XML

Now that you know the basics, this article explains how to use XML's more advanced constructs to author complex XML documents. Entities, namespaces, CDATA blocks, processing instructions - they're all in here, together with aliens, idiots, secret agents and buried treasure.

TABLE OF CONTENTS:
  1. XML Basics (part 2)
  2. Splitting Up
  3. Eating Humble PI
  4. XML And Alcohol
  5. The Name Game
  6. An Entity In The Attic
  7. Digging For Treasure
  8. The Man From IDIOT
  9. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 5
July 28, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
XML entities are a bit like variables in other programming languages - they're XML constructs which are referenced by a name and store text, images and file references. Once an entity has been defined, XML authors may call it by its name at different places within an XML document, and the XML parser will replace the entity name with its actual value.

XML entities come in particularly handy if you have a piece of text which recurs at different places within a document - examples would be a name, an email address or a standard header or footer. By defining an entity to hold this recurring data, XML allows document authors to make global alternations to a document by changing a single value.

Consider the following simple example:

<?xml version="1.0"?> <!DOCTYPE article [ <!ENTITY copyright "This material copyright Melonfire, 2001. All rights reserved."> ]> <article> <title>XML Basics (part 2)</title> <abstract>A discussion of basic XML theory</abstract> <body> &copyright; Article body goes here &copyright; </body> </article>
Entities come in two parts. First comes the entity definition, which always appears within the document type declaration at the head of the document (after the prolog). In this case, the entity "copyright" has ben defined and mapped to the string "This material copyright Melonfire, 2001. All rights reserved."

<!ENTITY copyright "This material copyright Melonfire, 2001. All rights reserved.">
Once an entity has been defined, the next step is to use it. This is accomplished via entity references, placeholders for entity data within the document markup. Typically, an entity reference contains the entity name prefixed with either an ampersand (&) or a percentage (%) symbol and suffixed with a semi-colon(;), as below:

<body> &copyright; Article body goes here &copyright; </body>
When a parser reads an XML document, it replaces the entity references with the actual values defined in the document type declaration. So this document

<?xml version="1.0"?> <!DOCTYPE article [ <!ENTITY copyright "This material copyright Melonfire, 2001. All rights reserved."> ]> <article> <title>XML Basics (part 2)</title> <abstract>A discussion of basic XML theory</abstract> <body> &copyright; Article body goes here &copyright; </body> </article>
would look like this once a parser was through with it.

<?xml version="1.0" ?> <title>XML Basics (part 2)</title> <abstract>A discussion of basic XML theory</abstract> <body>This material copyright Melonfire, 2001. All rights reserved. Article body goes here This material copyright Melonfire, 2001. All rights reserved.</body> </article>
Note that entities must be declared before they are referenced, and must appear within the document type declaration. If a parser finds an entity reference without a corresponding entity declaration, it will barf and produce some nasty error messages.

XML comes with the following five pre-defined entities:

&lt; - represents the less-than (<) symbol.

&gt; represents the greater-than (>) symbol

&apos; represents the single-quote (') symbol

&quote; represents the double-quote(") symbol

&amp; represents the ampersand (&) symbol

Entities can contain XML markup in addition to ordinary text - the following is a perfectly valid entity declaration:

<!ENTITY copyright "This material copyright <link>Melonfire</link>, <publication_year>2001</publication_year>. All rights reserved.">
Entities may be "nested"; one entity can reference another. Consider the following entity declaration, which illustrates this rather novel concept.

<!ENTITY company "Melonfire"> <!ENTITY year "2001"> <!ENTITY copyright "This material copyright &company;, &year;. All rights reserved.">
Note, however, that an entity cannot reference itself, either directly or indirectly, as this would result in an infinite loop (most parsers will warn you about this.) And now that I've said it, I just know that you're going to try it out to see how much damage it causes.

This article copyright Melonfire 2001. All rights reserved.

 
 
>>> More XML Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: