📄 5a_dtd.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--NewPage-->
<html>
<head>
<title>Document Type</title>
<style type="text/css">
<!--
-->
</style></head>
<body BGCOLOR="#ffffff">
<table width="100%">
<tr>
<td align=left> <a href="4_refs.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=bottom border=0 alt="Previous | "></a><a
href="5b_dtd.html"><img src="../images/NextArrow.gif" width=26 height=26 align=bottom border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=bottom border=0 alt="Index | "></a><a href="../TOC.html"><img
src="../images/xml_TOC.gif" width=26 height=26 align=bottom border=0 alt="TOC | "></a><a href="../index.html"><img
src="../images/xml_Top.gif" width=26 height=26 align=bottom border=0 alt="Top | "></a></td>
<td align=right><strong><em><a href="index.html">Top</a></em></strong>
<a href="../TOC.html#intro"><strong><em>Contents</em></strong></a> <a href="../alphaIndex.html"><strong><em>Index</em></strong></a> <a href="../glossary.html"><strong><em>Glossary</em></strong></a>
</td>
</tr>
</table>
<p>
<center>
<IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202"
HEIGHT="25" NATURALSIZEFLAG="3"> <IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202"
HEIGHT="25" NATURALSIZEFLAG="3">
</center>
<blockquote>
<blockquote>
<hr size=4>
</blockquote>
</blockquote>
<p>
<h2> 5a. Creating a Document Type Definition (DTD)</h2>
<table width="40%" border="1" align="right">
<tr>
<td>
<div align="center"><b><i>Link Summary</i></b></div>
</td>
</tr>
<tr>
<td>
<dl>
<dt><b><i>Exercise Links</i></b></dt>
</dl>
<ul>
<li><a href="samples/slideshow1a.dtd">slideshow1a.dtd</a></li>
<li><a href="samples/slideSample05.xml">slideSample05.xml</a></li>
</ul>
<dl>
<dt><b><i>API Links</i></b></dt>
</dl>
<ul>
<li><a href="../../api/internal/com/sun/xml/parser/Resolver.html">Resolver</a></li>
</ul>
<dl>
<dt><b><i>Glossary Terms</i></b></dt>
</dl>
<dl>
<dd><a href="../glossary.html#declaration">declaration</a>, <a href="../glossary.html#DTD">DTD</a>,
<a href="../glossary.html#externalSubset">external subset</a>, <a href="../glossary.html#localSubset">local
subset</a>, <a href="../glossary.html#mixedContent">mixed-content model</a>,
<a href="../glossary.html#prolog">prolog</a>, <a href="../glossary.html#root">root</a>,
<a href="../glossary.html#URL">URL</a>, <a href="../glossary.html#URN">URN</a>,
<a href="../glossary.html#valid">valid</a></dd>
</dl>
</td>
</tr>
</table>
<p>After the XML <a href="../glossary.html#declaration">declaration</a>, the document
<a href="../glossary.html#prolog">prolog</a> can include a <a href="../glossary.html#DTD">DTD</a>,
which lets you specify the kinds of tags that can be included in your XML document.
In addition to telling a validating parser which tags are <a href="../glossary.html#valid">valid</a>,
and in what arrangements, a DTD tells both validating and nonvalidating parsers
where text is expected, which lets the parser determine whether the whitespace
it sees is significant or ignorable.
<h3><a name="basic"></a>Basic DTD Definitions</h3>
<p>When you were parsing the slide show, for example, you saw that the <code>characters</code>
method was invoked multiple times before and after comments and slide elements.
In those cases, the whitespace consisted of the line endings and indentation
surrounding the markup. The goal was to make the XML document readable -- the
whitespace was not in any way part of the document contents. To begin learning
about DTD definitions, let's start by telling the parser where whitespace is
ignorable.
<blockquote>
<p><b>Note: </b>The DTD defined in this section is contained in <a href="samples/slideshow1a.dtd"><code>slideshow1a.dtd</code></a>.</p>
</blockquote>
<p>Start by creating a file named <code>slideshow.dtd</code><b>.</b> Enter an
XML declaration and a comment to identify the file, as shown below:
<blockquote>
<pre><?xml version='1.0' encoding='us-ascii'?>
<!-- DTD for a simple "slide show". -->
</pre>
</blockquote>
<p>Next, add the text highlight below to specify that a <code>slideshow</code>
element contains <code>slide</code> elements and nothing else:
<blockquote>
<pre><!-- DTD for a simple "slide show". -->
<b><!ELEMENT slideshow (slide+)></b></pre>
</blockquote>
<p>As you can see, the DTD tag starts with <code><!</code> followed by the
tag name (<code>ELEMENT</code>). After the tag name comes the name of the element
that is being defined (<code>slideshow</code>) and, in parentheses, one or more
items that indicate the valid contents for that element. In this case, the notation
says that a <code>slideshow</code> consists of one or more <code>slide</code>
elements. </p>
<p>Without the plus sign, the definition would be saying that a <code>slideshow</code>
consists of a single <code>slide</code> element. Here are the qualifiers you
can add to an element definition:</p>
<blockquote>
<table width="75%" border="1">
<tr>
<td width="19%" height="20"><i><b>Qualifier</b></i></td>
<td width="27%" height="20"><b><i>Name</i></b></td>
<td width="54%" height="20"><i><b>Meaning</b></i></td>
</tr>
<tr>
<td width="19%">
<div align="center"><b>?</b></div>
</td>
<td width="27%">Question Mark</td>
<td width="54%">Optional (zero or one)</td>
</tr>
<tr>
<td width="19%">
<div align="center"><b>*</b></div>
</td>
<td width="27%">Asterisk </td>
<td width="54%">Zero or more</td>
</tr>
<tr>
<td height="20" width="19%">
<div align="center"><b>+</b></div>
</td>
<td height="20" width="27%">Plus Sign</td>
<td height="20" width="54%">One or more</td>
</tr>
</table>
</blockquote>
<p>You can include multiple elements inside the parentheses in a comma separated
list, and use a qualifier on each element to indicate how many instances of
that element may occur. The comma-separated list tells which elements are valid
and the order they can occur in.</p>
<p>You can also nest parentheses to group multiple items. For an example, after
defining an <code>image</code> element (coming up shortly), you could declare
that every <code>image</code> element must be paired with a <code>title</code>
element in a slide by specifying <code>((image, title)+)</code>. Here, the plus
sign applies to the <code>image/title</code> pair to indicate that one or more
pairs of the specified items can occur.</p>
<h3><a name="text"></a>Defining Text and Nested Elements</h3>
<p>Now that you have told the parser something about where <i>not</i> to expect
text, let's see how to tell it where text <i>can</i> occur. Add the text highlighted
below to define the <code>slide</code>, <code>title</code>, <code>item</code>,
and <code>list</code> elements:</p>
<blockquote>
<pre>
<!ELEMENT slideshow (slide+)><b><br><!ELEMENT slide (title, item*)>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -