📄 ch02_11.htm
字号:
<html><head><title>Schemas (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_10.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_12.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.11. Schemas</h2><p>Several proposed alternate language schemas address the shortcomingsof DTD declarations. The W3C's recommended languagefor doing this is called <a name="INDEX-149" /><a name="INDEX-150" />XML Schema. You should know, however, thatit is only one of many competing schema-type languages, some of whichmay be better suited to your needs. If you prefer to use a competingschema, check CPAN to see if a module has been written to handle yourfavorite flavor of schemas.</p><p>Unlike DTD syntax, XML Schemas are themselves XML documents, makingit possible to use many XML tools to edit them. Their real power,however, is in their fine-grained control over the form your datatakes. This control makes it more attractive for documents for whichchecking the quality of data is at least as important as ensuring ithas the proper structure. <a href="ch02_11.htm#perlxml-CHP-2-EX-4">Example 2-4</a> shows a schemadesigned to model census forms, where data type checking isnecessary.</p><a name="perlxml-CHP-2-EX-4" /><div class="example"><h4 class="objtitle">Example 2-4. An XML schema </h4><blockquote><pre class="code"><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"> <xs:annotation> <xs:documentation> Census form for the Republic of Oz Department of Paperwork, Emerald City </xs:documentation> </xs:annotation> <xs:element name="census" type="CensusType"/> <xs:complexType name="CensusType"> <xs:element name="censustaker" type="xs:decimal" minoccurs="0"/> <xs:element name="address" type="Address"/> <xs:element name="occupants" type="Occupants"/> <xs:attribute name="date" type="xs:date"/> </xs:complexType> <xs:complexType name="Address"> <xs:element name="number" type="xs:decimal"/> <xs:element name="street" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="province" type="xs:string"/> <xs:attribute name="postalcode" type="PCode"/> </xs:complexType> <xs:simpleType name="PCode" base="xs:string"> <xs:pattern value="[A-Z]-d{3}"/> </xs:simpleType> <xs:complexType name="Occupants"> <xs:element name="occupant" minOccurs="1" maxOccurs="20"> <xs:complexType> <xs:element name="firstname" type="xs:string"/> <xs:element name="surname" type="xs:string"/> <xs:element name="age"> <xs:simpleType base="xs:positive-integer"> <xs:maxExclusive value="200"/> </xs:simpleType> </xs:element> </xs:complexType> </xs:element> </xs:complexType></xs:schema></pre></blockquote></div><p>The first line identifies this document as a schema and associates itwith the XML Schema namespace. The next structure,<tt class="literal"><annotation></tt>, is a place to document theschema's purpose and other details. After thisdocumentation, we get into the fun stuff and start declaring elementtypes.</p><p>We start by declaring the root of our document type, an elementcalled <tt class="literal"><census></tt>. The declaration is anelement of type <tt class="literal"><xs:element></tt>. Its attributesassign the name "census" and typeof description for <tt class="literal"><census></tt>,"CensusType". In schemas, unlikeDTDs, the content descriptions are often kept separate from thedeclarations, making it easier to define generic element types andassign multiple elements to them. Further down in the schema is theactual content description, an<tt class="literal"><xs:complexType></tt> element with<tt class="literal">name="CensusType"</tt>. It specifies that a<tt class="literal"><census></tt> contains an optional<tt class="literal"><censustaker></tt>, followed by a required<tt class="literal"><occupants></tt> and a required<tt class="literal"><address></tt>. It also must have an attributecalled <tt class="literal">date</tt>.</p><p>Both the attribute <tt class="literal">date</tt> and the element<tt class="literal"><censustaker></tt> have specific data patternsassigned in the description of <tt class="literal"><census></tt>: adate and a decimal number. If your <tt class="literal"><census></tt>document had anything but a numerical value as its content, it wouldbe an error according to this schema. You couldn'tget this level of control with DTDs.</p><p>Schemas can check for many types. These types include numericalvalues like bytes, floating-point numbers, long integers, binarynumbers, and boolean values; patterns for marking times anddurations; Internet addresses and URLs; IDs, IDREFs, and other typesborrowed from DTDs; and strings of character data.</p><p>An element type description uses properties called<em class="emphasis">facets</em><a name="INDEX-151" /> to set even more detailedlimits on content. For example, the schema above gives the<tt class="literal"><age></tt> element, whose data type is<tt class="literal">positive-integer</tt>, a maximum value of 200 using the<tt class="literal">max-inclusive</tt> facet. XML Schemas have many otherfacets, including <tt class="literal">precision</tt>,<tt class="literal">scale</tt>, <tt class="literal">encoding</tt>,<tt class="literal">pattern</tt>, <tt class="literal">enumeration</tt>, and<tt class="literal">max-length</tt>.</p><p>The <tt class="literal">Address</tt> description introduces a new concept:user-defined patterns. With this technique, we define<tt class="literal">postalcode</tt> with a pattern code:<tt class="literal">[A-Z]-d{3}</tt>. Using this code is like saying,"Accept any alphabetic character followed by a dashand three digits." If no data type fits your needs,you can always make up a new one.</p><p>Schemas are an exciting new technology that makes XML more useful,especially with data-specific applications such as data entry forms.We'll leave a full account of its uses and forms foranother book.</p><a name="perlxml-CHP-2-SECT-11.1" /><div class="sect2"><h3 class="sect2">2.11.1. Other Schema Strategies</h3><p>While it has the blessing of the W3C, XML Schema is not the onlyschema option available for flexible document validation. Someprogrammers prefer the methods available through specifications likeRelaxNG (available at<a href="http://www.oasis-open.org/committees/relax-ng/">http://www.oasis-open.org/committees/relax-ng/</a>)or Schematron(<a href="http://www.ascc.net/xml/resource/schematron/schematron.html">http://www.ascc.net/xml/resource/schematron/schematron.html</a>),which achieve the same goals through different philosophical means.Since the latter specification has Perl implementations that arecurrently available<a name="INDEX-152" /> <a name="INDEX-153" />, we'll examine itfurther in <a href="ch03_01.htm">Chapter 3, "XML Basics: Reading and Writing"</a>.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_10.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_12.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.10. Declaring Elements and Attributes</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.12. Transformations</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 + -