XML
  Home arrow XML arrow Page 6 - XML Basics (part 2)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XML Basics (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2001-07-28

    Table of Contents:
  • XML Basics (part 2)
  • Splitting Up
  • Eating Humble PI
  • XML And Alcohol
  • The Name Game
  • An Entity In The Attic
  • Digging For Treasure
  • The Man From IDIOT
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    XML Basics (part 2) - An Entity In The Attic


    (Page 6 of 9 )

    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


     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway