📄 0131-0134.html
字号:
<!DOCTYPE HTML PUBLIC "html.dtd"><HTML><HEAD><TITLE>Presenting XML:Keeping It Tidy: The XML Rule Book:EarthWeb Inc.-</TITLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><SCRIPT><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');}//--></SCRIPT></HEAD><BODY BGCOLOR="#FFFFFF" VLINK="#DD0000" TEXT="#000000" LINK="#DD0000" ALINK="#FF0000"><TD WIDTH="540" VALIGN="TOP"><!-- <CENTER><TABLE><TR><TD><FORM METHOD="GET" ACTION="http://search.itknowledge.com/excite/cgi-bin/AT-foldocsearch.cgi"><INPUT NAME="search" SIZE="20" VALUE=""><BR><CENTER><INPUT NAME="searchButton" TYPE="submit" VALUE="Glossary Search"></CENTER><INPUT NAME="source" TYPE="hidden" VALUE="local" CHECKED> <INPUT NAME="bltext" TYPE="hidden" VALUE="Back to Search"><INPUT NAME="sp" TYPE="hidden" VALUE="sp"></FORM></TD><TD><IMG SRC="http://www.itknowledge.com/images/dotclear.gif" WIDTH="15" HEIGHT="1"></TD><TD><FORM METHOD="POST" ACTION="http://search.itknowledge.com/excite/cgi-bin/AT-subscriptionsearch.cgi"><INPUT NAME="search" SIZE="20" VALUE=""><BR><CENTER><INPUT NAME="searchButton" TYPE="submit" VALUE=" Book Search "></CENTER><INPUT NAME="source" TYPE="hidden" VALUE="local" CHECKED> <INPUT NAME="backlink" TYPE="hidden" VALUE="http://search.itknowledge.com:80/excite/AT-subscriptionquery.html"><INPUT NAME="bltext" TYPE="hidden" VALUE="Back to Search"><INPUT NAME="sp" TYPE="hidden" VALUE="sp"></FORM></TD></TR></TABLE></CENTER> --><!-- ISBN=1575213346 //--><!-- TITLE=Presenting XML//--><!-- AUTHOR=Richard Light//--><!-- PUBLISHER=Macmillan Computer Publishing//--><!-- IMPRINT=Sams//--><!-- CHAPTER=08 //--><!-- PAGES=0123-0146 //--><!-- UNASSIGNED1 //--><!-- UNASSIGNED2 //--><P><CENTER><A HREF="0127-0130.html">Previous</A> | <A HREF="../ewtoc.html">Table of Contents</A> | <A HREF="0135-0138.html">Next</A></CENTER></P><A NAME="PAGENUM-131"><P>Page 131</P></A><H4><A NAME="ch08_ 12">Mixed Content</A></H4><P>If you want an element to be able to contain character data, different rulesapply. This is called mixed content (even when there is just character data, notmixed with any subelements).</P><P>To express the rules for mixed content, you use the preceding contentmodel grammar. However, you can use it only in a particular way. The contentmodel has to take the form of a single set of alternatives, starting with#PCDATA and followed by the element types that can occur in the mixed content, eachdeclared only once. Except when #PCDATA is the only option, the* qualifier must follow the closing bracket, as in this example:</P><!-- CODE SNIP //--><PRE><!ELEMENT x (#PCDATA | a | b | c)*><!ELEMENT x (#PCDATA)></PRE><!-- END CODE SNIP //--><P>It is worth being clear about the limits of this process. XML's contentmodels relate only to the structure of an XML document. They make no attemptto control its content. In fact, even an element that is totally devoid of datamatches a #PCDATA content model:</P><!-- CODE SNIP //--><PRE><p></p></PRE><!-- END CODE SNIP //--><P>This is a valid paragraph. If you require any form of data validation supportin your XML applications, you will have to add this support in anapplication-specific manner.</P><TABLE BGCOLOR="#FFFF99"><TR><TD>Note:</TD></TR><TR><TD><BLOCKQUOTE>Typing of XML elements is being actively discussed, and it is likelythat a mechanism for specifying allowed data types within XML will be developed.</BLOCKQUOTE></TD></TR></TABLE><BR><BR><TABLE BGCOLOR="#FFFF99"><TR><TD>Warning:</TD></TR><TR><TD><BLOCKQUOTE>It is possible to write content models that are not valid in XMLby including #PCDATA in a model that does not follow the allowed patternfor mixed content. A well-behaved XML processor will complain aboutsuch errors. Be warned, however, that an SGML parser will not complain,because such constructions are valid (although deprecated)in SGML.</BLOCKQUOTE></TD></TR></TABLE><A NAME="PAGENUM-132"><P>Page 132</P></A><H3><A NAME="ch08_ 13">Attribute List Declarations</A></H3><P>Having declared an element and learned where it can fit into the structureof documents conforming to your DTD, you can now give that element oneor more attributes. To do this, you include attribute listdeclarations (called a list because each element can have a number of attributes, which are listed ina single declaration).</P><P>In XML documents, attribute specifications occur in start-tags and consistof an attribute name followed by a value:</P><!-- CODE SNIP //--><PRE><head level="subhead2"></PRE><!-- END CODE SNIP //--><P>The preceding assigns the value"subhead2" to the attribute level. In theDTD, your attribute specification list will do the following for each element:</P><UL><LI> Declare the names of allowed attributes.<LI> State the type of each attribute.<LI> Provide a default value for each attribute.</UL><P>This example declares the attributes for theperson element:</P><!-- CODE SNIP //--><PRE><!ATTLIST PERSONID ID #IMPLIEDROLE CDATA #IMPLIEDKEY CDATA #IMPLIEDTEIFORM CDATA #FIXED "PERSON"></PRE><!-- END CODE SNIP //--><H4><A NAME="ch08_ 14">Types of Attributes</A></H4><P>In general terms, there are three types of attributes:</P><UL><LI> String attributesconsist of any character data and always have the declared typeCDATA.<LI> Tokenized attributesconsist of one or more tokens that are significant to XML.<LI> Enumerated attributesare one of a specific list of possible values declared in the DTD.</UL><P>String-type attributes are the most common. The following is the form touse when you don't have a more specific purpose for the attribute:</P><!-- CODE SNIP //--><PRE>ROLE CDATA #IMPLIEDKEY CDATA #IMPLIED</PRE><!-- END CODE SNIP //--><A NAME="PAGENUM-133"><P>Page 133</P></A><P>Tokenized attributes have one of the following types:</P><UL><LI> ID: An identifier for the element that, if specified, must beunique within the document. Consists of aNamestart character, followed by a sequence ofName characters.<LI> IDREF: A pointer to anID. Must match an actual ID declared somewhere in the same document.<LI> IDREFS: LikeIDREF, except that the value can consist ofmultiple IDREFs separated by space.<LI> ENTITY: A pointer to an external binary general entity (defined inthe section "Text and Binary Entities," later in this chapter) that hasbeen declared in the DTD. Consists of name characters only.<LI>NTITIES: Like ENTITY, except that the value can consist ofmultiple ENTITYs separated by space.<LI>NMTOKEN: A string that takes the form of a NMTOKEN.<LI>NMTOKENS: Like NMTOKEN, except that the value can consist ofmultiple NMTOKENs separated by space.</UL><TABLE BGCOLOR="#FFFF99"><TR><TD>Note:</TD></TR><TR><TD><BLOCKQUOTE>NMTOKENS were defined in Chapter 5, "The XML Approach,"under the heading "Primitive Constructs."</BLOCKQUOTE></TD></TR></TABLE><BR><BR><TABLE BGCOLOR="#FFFF99"><TR><TD>Warning:</TD></TR><TR><TD><BLOCKQUOTE>The ID attribute type can be applied in principle to anyattribute. However, it is standard practice to restrict its use to an attribute that isalso called ID.</BLOCKQUOTE></TD></TR></TABLE><P>Enumerated attributes simply take a list of possible values. The onlyrestriction is that each value has to be a validNMTOKEN. Here is an example:</P><!-- CODE SNIP //--><PRE><!ATTLIST boxSHAPE (ROUND | OVAL | SQUARE) "SQUARE"></PRE><!-- END CODE SNIP //--><TABLE BGCOLOR="#FFFF99"><TR><TD>Warning:</TD></TR><TR><TD><BLOCKQUOTE>For compatibility with SGML, you should not use the sameNMTOKEN more than once in the enumerated attribute types of a single element type.Although this restriction has now been removed from the SGML standard,the</BLOCKQUOTE></TD></TR></TABLE><A NAME="PAGENUM-134"><P>Page 134</P></A><TABLE BGCOLOR="#FFFF99"><TR><TD></TD></TR><TR><TD><BLOCKQUOTE>existing base of SGML-aware software will continue to enforce it. However,if you do use the same NMTOKEN within the enumerated attribute types of asingle element type, no XML processor will complain.</BLOCKQUOTE></TD></TR></TABLE><P>It is a special situation when the enumerated list gives a set of possiblenotations. In this case, the notations must all have been declared, and the listis preceded by NOTATION. Here is an example:</P><!-- CODE SNIP //--><PRE><!ATTLIST IMAGEFORMAT NOTATION (GIF | JPEG | BMP) "GIF"></PRE><!-- END CODE SNIP //--><P>When matching an attribute value against the allowed values specified inthe attribute definition, an XML processor will carry out a case-insensitivematch for all attributes, except those of type CDATA,IDREF, or IDREFS.</P><H4><A NAME="ch08_ 15">Default Actions</A></H4><P>The last part of the declaration of each attribute states which action theXML processor should take when users choose (as they often will) not toprovide that attribute in a particular start-tag.</P><P>Here are the three options for the last part of the declaration:</P><UL><LI>#REQUIRED means the attribute should have been there. Missingit makes the document invalid.<LI>#IMPLIED has the XML processor tell the application that no valuewas specified.<LI> The declared default value is passed through to the applicationas though that value had actually been specified. If the default valueis preceded by #FIXED, any value that is specified must match thedefault value, or the document will be invalid.</UL><TABLE BGCOLOR="#FFFF99"><TR><TD>Warning:</TD></TR><TR><TD><BLOCKQUOTE>The default value, like all actual attribute values, must bequoted. This even applies to default values of enumerated attributes. For example,the following attribute definition (taken from the HTML 3.2 DTD) would notbe valid in XML:<BR><!-- CODE SNIP //--><PRE><!ATTLIST BRclear (left|all|right|none) none -- control of text flow --></PRE><!-- END CODE SNIP //--><BR>The default value should read "none".</BLOCKQUOTE></TD></TR></TABLE><P><CENTER><A HREF="0127-0130.html">Previous</A> | <A HREF="../ewtoc.html">Table of Contents</A> | <A HREF="0135-0138.html">Next</A></CENTER></P></TD></TR></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -