<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_plain.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="m9002">
  <name>XML Basics</name>
  <metadata>
  <md:version>2.23</md:version>
  <md:created>2000/05/18</md:created>
  <md:revised>2008/05/10 10:54:18.221 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="selc">
      <md:firstname>Sarah</md:firstname>
      
      <md:surname>Coppin</md:surname>
      <md:email>coppin@alumni.rice.edu</md:email>
    </md:author>
      <md:author id="brentmh">
      <md:firstname>Brent</md:firstname>
      <md:othername>Michael</md:othername>
      <md:surname>Hendricks</md:surname>
      <md:email>brentmh@rice.edu</md:email>
    </md:author>
      <md:author id="cbearden">
      <md:firstname>Charles</md:firstname>
      <md:othername>F.</md:othername>
      <md:surname>Bearden</md:surname>
      <md:email>cbearden@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="brentmh">
      <md:firstname>Brent</md:firstname>
      <md:othername>Michael</md:othername>
      <md:surname>Hendricks</md:surname>
      <md:email>brentmh@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="cbearden">
      <md:firstname>Charles</md:firstname>
      <md:othername>F.</md:othername>
      <md:surname>Bearden</md:surname>
      <md:email>cbearden@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="jago">
      <md:firstname>Adan</md:firstname>
      
      <md:surname>Galvan</md:surname>
      <md:email>agalvan@gmail.com</md:email>
    </md:maintainer>
    <md:maintainer id="charlet">
      <md:firstname>Charlet</md:firstname>
      
      <md:surname>Reedstrom</md:surname>
      <md:email>charlet@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>attribute</md:keyword>
    <md:keyword>entity</md:keyword>
    <md:keyword>nesting</md:keyword>
    <md:keyword>unicode</md:keyword>
    <md:keyword>valid</md:keyword>
    <md:keyword>well-formed</md:keyword>
    <md:keyword>xml</md:keyword>
  </md:keywordlist>

  <md:abstract>This module describes XML (eXtensible Markup Language) and the rules that govern its usage.  It also explains what a well-formed and valid document is.</md:abstract>
</metadata>

  <content>
    
    <section id="whatxml">
      <name>What is XML?</name>
      <para id="pa1">
	The eXtensible Markup Language (<term>XML</term>) is a
	<term>meta-markup language</term> defined by the <link src="http://www.w3.org">World Wide Web Consortium
	(W3C)</link>.  It is not strictly a markup language itself,
	but rather a set of rules for creating markup languages.  For
	our purposes a <term>markup language</term> is any language
	(HTML, for example) that uses tags surrounding text to convey
	information such as content or format.  <link src="http://cnx.rice.edu/cnxml">CNXML</link>, the markup
	language used by the <link src="http://cnx.rice.edu">Connexions Project</link> is an
	example of a language written in XML.  There are many other
	examples at the W3C site.  Here is an example of some markup
	in CNXML.
      </para>
      <example id="ex1">
      <code type="block">
<![CDATA[<para>
  This is a paragraph in <term>CNXML</term>.  Notice that the markup
  contains tags that express the meaning of the text.
</para>]]>
      </code>
      </example>
      <para id="pa5"><code type="inline">&lt;para&gt;</code> and
	<code type="inline">&lt;/para&gt;</code> are the tags that 
	enclose the text.  In XML, tags are always marked by angle
	brackets (also known as <code type="inline">&lt;</code> and
	<code type="inline">&gt;</code>).  <term>Tags</term> generally come
	in pairs.  An opening tag will look like
	<code type="inline">&lt;tagname&gt;</code>.  A closing tag will look
	like <code type="inline">&lt;/tagname&gt;</code>, with a
	<code type="inline">/</code> preceding the tag name.
      </para>
      <para id="pa2">
	XML allows the separation of presentation from content.  For example, HTML
	has tags such as <code type="inline">&lt;u&gt;</code> and
	<code type="inline">&lt;i&gt;</code>, which underline and italicize
	text respectively.  This does not express content information,
	only formatting.  XML allows you to define your own language
	of tags to represent content.  You could create a tag called
	<code type="inline">&lt;book&gt;</code> to represent book titles,
	and create a stylesheet (a separate formatting document), that
	says that every <code type="inline">&lt;book&gt;</code> tag should
	be italicized or underlined.  Then when you want to change the
	presentation of that type of content, you just change one
	small part of the stylesheet.  Also, if you make tags that
	convey the content of the document, you can enable better
	searching.  For example, you might look for the author of a
	document by looking at the author tag.
      </para>
    </section>


    <section id="xmlrules">
      <name>Well-formed XML</name>
      <para id="pa3">XML has a few rules that apply to all of its
      languages, including CNXML. If a document satisfies these
      rules, then it is <term>well-formed</term>.  XML documents are
      required to be well-formed.
	<list id="threerules">
	  <item>
	    Every tag that is opened must be closed.  An opening tag
	    looks like <code type="inline">&lt;module&gt;</code> and a
	    closing tag looks like
	    <code type="inline">&lt;/module&gt;</code>.  There is a
	    shortcut.  If your tag contains no other tags (referred to
	    as an <term>empty tag</term>), then you can can type a /
	    before the end of the opening tag and delete the closing
	    tag.  For example,
	    <code type="inline">&lt;media&gt; &lt;/media&gt;</code>
	    can be abbreviated
	    <code type="inline">&lt;media/&gt;</code>.
	  </item>
	  <item>
	    Tags must be nested within each other.  So,
	    <code type="inline">&lt;b&gt;red &lt;i&gt;and&lt;/i&gt;
	    blue&lt;/b&gt;</code> is fine, but
	    <code type="inline">&lt;b&gt;red &lt;i&gt;and&lt;/b&gt;
	    blue&lt;/i&gt;</code>is incorrect because the
	    <code type="inline">&lt;b&gt;</code> and
	    <code type="inline">&lt;i&gt;</code> tags have overlapping
	    content.
	  </item>
	  <item>
	    You must put either single or double quotes around an
	    attribute value.  An <term>attribute</term> is some sort
	    of information that is associated with a tag and is listed
	    inside of the tag itself.  For example,
	  <code type="inline">
	    &lt;module id="m0001"&gt;
	  </code>
	  and
	  <code type="inline">
	    &lt;module id='m0001'&gt;
	  </code>
	  are fine, but
	  <code type="inline">
	    &lt;module id=m0001&gt;
	  </code>
	  is incorrect.
	  </item>
	  
	  <item>
	    You can also choose to start every document with an
	    <term>XML declaration</term>.  If you do use the XML
	    declaration, then it has to be the very first thing in the
	    file.  It cannot even be preceded by whitespace.  It is
	    not considered to be a tag.  The XML declaration is as
	    follows.  <code type="inline">&lt;?xml version="1.0"?&gt;</code>
	    You can also include other information such as the
	    encoding of the document or whether the document depends
	    on other files or not.
	  </item>
	  <item>
	    There must be one tag that contains all of the other tags.  For
	    example in xhtml <code type="inline">&lt;html&gt;</code> and
	    <code type="inline">&lt;/html&gt;</code> must surround all of
	    the other tags.  There are some things that are included
	    at the top of the document that are not tags and that are
	    not included with the tags.  The XML declaration is an
	    example of this.
	  </item>	  
	</list>
      </para>
    </section>


    <section id="valid">
      <name>Valid XML</name>
      <para id="pvalid">
	It is possible to define a set of rules that apply to all of
	the tags in a particular XML language.  These rules can be
	defined in a couple of different ways. The most common way is
	to use a <term>DTD</term> (Document Type Definition).  Any
	document which follows all of the rules for that language is
	called <term>valid</term>.  A document is not required to be
	valid in order to be XML.  However, it is generally a good
	idea.
      </para>
    </section>
    <section id="entity-refs">
      <name>Entity References</name>
      <para id="pent1">
XML uses several characters in special ways as part of its markup, in particular
the less-than symbol (<emphasis>&lt;</emphasis>),
the greater-than symbol (<emphasis>&gt;</emphasis>),
the double quotation mark (<emphasis>"</emphasis>),
the apostrophe (<emphasis>'</emphasis>),
and the ampersand (<emphasis>&amp;</emphasis>).
You've already seen examples of markup using the first four of those previously in this module.
But what if you need to these characters in your content, and you don't want them to be treated as part of the markup by XML processors?  You can use XML <term>entity references</term> for this purpose.  The XML Specification defines the following five entity references for use in any well-formed XML document:
        <list id="entref-list" type="bulleted">
          <item><code>&amp;amp;</code> refers to an ampersand (&amp;)</item>
          <item><code>&amp;lt;</code> refers to a less-than symbol (&lt;)</item>
          <item><code>&amp;gt;</code> refers to a greater-than symbol (&gt;)</item>
          <item><code>&amp;quot;</code> refers to a double-quote mark (")</item>
          <item><code>&amp;apos;</code> refers to an apostrophe (')</item>
        </list>
      </para>
      <example id="exent2">
        <para id="pent2">
          Suppose you have a document with the following:
          <code type="block"><![CDATA[
      <para id="p1">The firm was known as Scrooge and Marley.</para>]]>
          </code>
          you could replace 'and' with the entity reference <code>&amp;amp;</code>:
          <code type="block"><![CDATA[
      <para id="p1">The firm was known as Scrooge &amp; Marley.</para>]]>
          </code>
  	<!-- An <term>entity</term> is a way to escape characters.
  	Entities always have the same basic form.  An entity will
  	start with an ampersand and end with a semicolon.  For
  	example, &amp;amp; will escape the &amp; character.  So if you
  	type &amp;amp; in the source file, you will see &amp; when you
  	view it in a browser.  There are several entities that are
  	predefined and will work in any XML file.  They are &amp;amp
  	(&amp;), &amp;lt (&lt;), &amp;gt (&gt;), &amp;quot
  	("), and &amp;apos (').  These are the same
  	entities that are defined for HTML.  In addition to these
  	basic symbols, you can define your own entities using a DTD.
  	These self defined entities can point to other symbols, or
  	they can be short cuts to a larger piece of text. -->
        </para>
      </example>
      <para id="pent3">All entity references outside the above five must be defined in a document type declaration, and they may only be used in documents that conform to that DTD.  Note that an entity reference always begins with <code>&amp;</code> and ends with <code>;</code>.
      </para>
    </section>
    <section id="char-refs">
      <name>Character References</name>
      <para id="cent1">
	You can also use any character defined in <term>Unicode</term> in an XML document by means of <term>character references</term>.
	Unicode is a project to define a unique code for every
	character in any human language. Unicode is very useful any
	time that you need to use a symbol that is not a part of
	ASCII.
      </para>
      <para id="cent2">
        Character references in XML either begin with <code>&amp;#</code>, or they begin with <code>&amp;#x</code>, and they end with a semicolon <code>;</code>.  A character reference contains a representation of a Unicode code point: if it begins with <code>&amp;#</code>, then it contains a decimal representation of a Unicode code point; if it begins with <code>&amp;#x</code>, then it contains a hexidecimal representation of a Unicode code point.
      </para>
      <example id="cent2-ex1">
        <para id="cent2-ex1-p1">The hexidecimal representation of the Unicode code point for the small 'o' with a stroke is <code>00F8</code>, and the decimal representation for the same is <code>248</code>.  Therefore, the character references for the small 'o' with a stroke are <code>&amp;#x00F8;</code> and <code>&amp;#248;</code>
        So you could write
          <code type="block"><![CDATA[
    <emphasis>The majestik m&#x00F8;&#x00F8;se</emphasis>]]>
          </code>
        or
          <code type="block"><![CDATA[
    <emphasis>The majestik m&#248;&#248;se</emphasis>]]>
          </code>
        or even 
          <code type="block"><![CDATA[
    <emphasis>The majestik m&#x00F8;&#248;se</emphasis>]]>
          </code>
        to get
          <quote type="block">
    The majestik møøse
          </quote>
        </para>
      </example>
	<!-- Unicode entities have an extra # sign.  So they look like:
	&amp;#134;.  Or if the code is in hexadecimal, there is also
	an extra x causing them to look like: &amp;#x03C0;. -->
    </section>
  </content>
</document>
