📄 ch02_10.htm
字号:
<html><head><title>Declaring Elements and Attributes (Perl and XML)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Erik T. Ray and Jason McIntosh" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="059600205XL" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl and XML" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl & XML" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_09.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch02_11.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.10. Declaring Elements and Attributes</h2><p>When<a name="INDEX-142" /><a name="INDEX-143" /> you need anextra level of quality control (beyond the healthful status impliedby the "well-formed" label), definethe grammar patterns of your markup language in the DTD. Defining thepatterns will make your markup into a formal language, documentedmuch like a standard published by an international organization. Witha DTD, a program can tell in short order whether a document conformsto, or, as we say, is a <em class="emphasis">valid</em> example of, yourdocument type.</p><p>Two kinds of declarations allow a DTD to model a language. The firstis the <em class="emphasis">elementdeclaration</em><a name="INDEX-144" />. It adds anew name to the allowed set of elements and specifies, in a specialpattern language, what can go inside the element. Here are someexamples:</p><blockquote><pre class="code"><!ELEMENT sandwich ((meat | cheese)+ | (peanut-butter, jelly)), condiment+, pickle?)><!ELEMENT pickle EMPTY><!ELEMENT condiment (PCDATA | mustard | ketchup )*></pre></blockquote><p>The first parameter declares the name of the element. The secondparameter is a pattern, a <em class="emphasis">contentmodel</em><a name="INDEX-145" /> in parentheses, or a keyword such as<tt class="literal">EMPTY</tt>. Content models resemble regular expressionsyntax, the main differences being that element names are completetokens and a comma is used to indicate a required sequence ofelements. Every element mentioned in a content model should bedeclared somewhere in the DTD.</p><p>The other important kind of declaration isthe<a name="INDEX-146" /> <em class="emphasis">attribute listdeclaration</em>. With it, you can declare a set of optionalor required attributes for a given element. The attribute values canbe controlled to some extent, though the pattern restrictions aresomewhat limited. Let's look at an example:</p><blockquote><pre class="code"><!ATTLIST sandwich id ID #REQUIRED price CDATA #IMPLIED taste CDATA #FIXED "yummy" name (reuben | ham-n-cheese | BLT | PB-n-J ) 'BLT'></pre></blockquote><p>The general pattern of an attribute declaration has three parts: aname, a data type, and a behavior. This example declares threeattributes for the element <tt class="literal"><sandwich></tt>. Thefirst, named <tt class="literal">id</tt>, is of type <tt class="literal">ID</tt>,which is a unique string of characters that can be used only once inany ID-type attribute throughout the document, and is requiredbecause of the <tt class="literal">#REQUIRED</tt> keyword. The second,named <tt class="literal">price</tt>, is of type <tt class="literal">CDATA</tt>and is optional, according to the <tt class="literal">#IMPLIED</tt>keyword. The third, named <tt class="literal">taste</tt>, is fixed with thevalue <tt class="literal">"yummy"</tt> and can't bechanged (all <tt class="literal"><sandwich></tt> elements willinherit this attribute automatically). Finally, the attribute<tt class="literal">name</tt> is one of an enumerated list of values, withthe default being <tt class="literal">'BLT'</tt>.</p><p>Though they have been around for a long time and have been verysuccessful, element and attribute declarations have some major flaws.Content model syntax is relatively inflexible. For example,it's surprisingly hard to express the statement"this element must contain one each of the elementsA, B, C, and D in any order" (try it and see!).Also, the character data can't be constrained in anyway. You can't ensure that a<tt class="literal"><date></tt> contains a valid date, and not astreet address, for example. Third, and most troubling for the XMLcommunity, is the fact that DTDs don't play wellwith namespaces. If you use element declarations, you have to declareall elements you would ever use in your document, not just some ofthem. If you want to leave open the possibility of importing someelement types from another namespace, you can't alsouse a DTD to validate your document -- at least not withoutplaying the mix-and-match DTD-combination games we described earlier,and combining DTDs doesn't always work<a name="INDEX-147" /> <a name="INDEX-148" />, anyway.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_09.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch02_11.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.9. Free-Form XML and Well-Formed Documents</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">2.11. Schemas</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -