|
Related Reading
Building Oracle XML Applications |
<?xml version="1.0"?>
The special tag delimiters of <? and ?> distinguish this declaration from other tags in the document. The <?xml characters in the XML declaration must be the very first characters in the document. No spaces or carriage returns or anything can come before them.
<?xml version="1.0"?>
<Question>Is this legal?</Question>
But the following is not:
<?xml version="1.0"?>
<Question>Is this legal?</Question>
<Answer>No</Answer>
because both <Question> and <Answer> are top-level elements. You can't even have the same element name repeated at the top level: there must be exactly one. So the following is also illegal:
<?xml version="1.0"?>
<Question>Is this legal?</Question>
<Question>Is that your final answer?</Question>
You need to pick a single name and use that element to enclose the others, like:
<?xml version="1.0"?>
<FAQ-List>
<Question>Is this legal?</Question>
<Question>Is that your final answer?</Question>
</FAQ-List>
<Question>Is this legal?</question>
<QUESTION>Is this legal?</Question>
You'll find that XML syntax is rigid and unforgiving. You cannot get away with being sloppy about the order of closing tags. The following is illegal:
<Question><Link href="http://qa.com/">Is this
legal?</Question></Link>
You need to close </Link> before closing </Question>, like this:
<Question><Link href="http://qa.com/">Is this
legal?</Link></Question>
Simply keeping your tags neatly indented helps you avoid this mistake:
<Question>
<Link href="http://qa.com/">Is this legal?</Link>
</Question>
Note that adding extra spaces, carriage returns, or tabs between nested tags to make an XML document look indented to the human eye does not affect its structural meaning when working with datagrams, although clearly it increases the document's size slightly.
<?xml version="1.0"?>
<!-- Comment Here ok -->
<FAQ-List>
<!-
| And here, multiple lines are fine
+-->
<Question>Is this legal?<!-- Here is fine --></Question>
<!-- Here too -->
<Answer>Yes</Answer>
</FAQ-List>
<!-- Even Here -->
but all four comments in this example are not:
<!-- NOT before XML declaration -->
<?xml version="1.0"?>
<FAQ-List>
<FAQ Submitter="<!-- NOT in an attribute value -->" >
<Question <!-- NOT between < and > of a tag --> >Is this
legal?</Question>
<Answer>Yes</Answer>
<!-- Illegal for comment to contain two hypens -- like this -->
</FAQ>
</FAQ-List>
<2-Part-Question> <!-- Error: element name starts with a digit --> <Two Part Question> <!-- Error: has spaces in the name --> <Question 4You="Yes"> <!-- Error: attribute name starts with a digit -->
Some punctuation symbols (like underscore and hyphen) are allowed in names, but most others are illegal:
<_StrangeButLegal>Legal</_StrangeButLegal>
<More-Normal-Looking>Legal</More-Normal-Looking>
<OK_As_Well>Legal</OK_As_Well>
<FAQ Submitter="smuench@oracle.com">
<!-- etc. -->
</FAQ>
while the following is illegal:
<FAQ>
<!-- etc. -->
</FAQ Submitter="smuench@oracle.com">
<FAQ Submitter="smuench@oracle.com">
<FAQ Submitter='smuench@oracle.com'>
but the following two are not. You can't forget the quotes:
<FAQ Submitter=smuench@oracle.com>
<FAQ Submitter='smuench@oracle.com">
or be sloppy about using the same closing quote character as your opening one.
<Task Subtasks="<Task Name='Learn XML Syntax'>"/>
<Company>AT & T</Company> <!-- AT & T --> <Where-Clause>SAL < 5000</Where-Clause> <!-- SAL < 500 -->
On occasion, the " and ' also come in handy to represent literal " and ' in attribute values:
<Button On-Click="alert('Print a " and '');"></Button>
<Task Name="Learn XML Syntax">
<Task Name="Use Empty Elements"/> <!-- Empty Element -->
</Task>
As shown above with the Name attribute on the empty <Task> element, attributes on empty elements are still legal.
Steve Muench is Oracle's lead XML Technical Evangelist and development lead for Oracle XSQL Pages.
Return to oracle.oreilly.com.
Copyright © 2009 O'Reilly Media, Inc.