Creating XML Documents

The World Wide Web Consortium (http://www.w3.org/)

What is a Well-Formed XML Document?

A textual object is a well-formed XML Document if:

  1. Taken as a whole, it matches the production labeled document
  2. It meets all the well-formedness contraints given in this specification:
    http://www.w3.org/TR/REC-xml
  3. Each of the parsed entities which is referenced directly or indirectly whitin the document is well-formed

document ::= prolog element Misc*

Entity

Tags and Elements

Attributes

Elements vs Attributes

Elements vs Attributes (2)

Too many attributes make documents hard to read:
    <CUSTOMER LAST_NAME="Smith" FIRST_NAME="Sam" 
    DATE="October 15, 2001" PURCHASE="Tomatoes" 
    PRICE="$1.25" NUMBER="8" />
    <CUSTOMER>
        <NAME>
            <LAST_NAME>Smith</LAST_NAME>
            <FIRST_NAME>Sam</FIRST_NAME>
        </NAME>
        <DATE>October 15, 2001</DATE>
        <ORDERS>
            <ITEM>
                <PRODUCT>Tomatoes</PRODUCT>
                <NUMBER>8</NUMBER>
                <PRICE>$1.25</PRICE>
            </ITEM>
        </ORDERS>
    </CUSTOMER>

Elements vs Attributes (3)

Building Well-Formed XML Document Structure

An XML Declaration should begin the document
<?xml version="1.0" standalone="yes"?>

Include one or more elements
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<GREETING>Hello from XML</GREETING>
<MESSAGE>Welcome to Programing XML in Java</MESSAGE>
</DOCUMENT>

Building Well-Formed XML Document Structure (2)

Include Both Start and End Tags for Elements that aren't empty
<GREETING>Hello from XML</GREETING>

Close Empty Tags with />
<SUBJECT name="XML"/>

The Root Element Must Contain All Other Elements
<DOCUMENT> ..... </DOCUMENT>

Building Well-Formed XML Document Structure (3)

Nest Elements Correctly:
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
   <GREETING>Hello from XML</MESSAGE>  <--- wrong end tag
   <MESSAGE>
   Welcome to Programing XML in Java
   </GREETING> <--- wrong end tag
</DOCUMENT>

Building Well-Formed XML Document Structure (4)

Use Unique Attribute Names:
<PERSON LAST_NAME="Smith" LAST_NAME="Punin">

Use Only the Five Pre-Existing Entity References:
&amp; the & character
&lt; the < character
&gt; the > character
&apos; the ` character
&quot; the " character

Building Well-Formed XML Document Structure (5)

Surround Attribute Values with Quotes:
<IMG SRC="image.jpg"/>

Use < and & Only to Start Tags and Entities:
<TOUR CAPTION="The S&amp;O Railway"/>

CDATA Sections

CDATA Sections (2)

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head>
        <title>Using The if Statement In JavaScript</title>
    </head>
    <body>
        <script language="javascript">
	  <![CDATA[
            var budget
            budget = 234.77
            if (budget < 0){
                document.writeln("Uh oh.")}
	  ]]>
        </script>
        <center> <h1>Using The if Statement In JavaScript</h1> </center>
    </body>
</html>