⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02_03.htm

📁 Perl & XML. by Erik T. Ray and Jason McIntosh ISBN 0-596-00205-X First Edition, published April
💻 HTM
字号:
<html><head><title>Namespaces (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 &amp; 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 &amp; 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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.3. Namespaces</h2><p>It's sometimes useful to divide up your elements andattributes into groups, or<em class="emphasis">namespaces</em><a name="INDEX-86" /><a name="INDEX-87" />. Anamespace is to an element somewhat as a surname is to a person. Youmay know three people named Mike, but no two of them have the samelast name. To illustrate this concept, look at the document in <a href="ch02_03.htm#perlxml-CHP-2-EX-2">Example 2-2</a>. </p><a name="perlxml-CHP-2-EX-2" /><div class="example"><h4 class="objtitle">Example 2-2. A document using namespaces </h4><blockquote><pre class="code">&lt;?xml version="1.0"?&gt;&lt;report&gt;  &lt;title&gt;Fish and Bicycles: A Connection?&lt;/title&gt;  &lt;para&gt;I have found a surprising relationship  of fish to bicycles, expressed by the equation   &lt;equation&gt;f = kb+n&lt;/equation&gt;. The graph below illustrates  the data curve of my experiment:&lt;/para&gt;  &lt;chart xmlns:graph="http://mathstuff.com/dtds/chartml/"&gt;    &lt;graph:dimension&gt;      &lt;graph:axis&gt;fish&lt;/graph:axis&gt;      &lt;graph:start&gt;80&lt;/graph:start&gt;      &lt;graph:end&gt;99&lt;/graph:end&gt;      &lt;graph:interval&gt;1&lt;/graph:interval&gt;    &lt;/graph:dimension&gt;    &lt;graph:dimension&gt;      &lt;graph:axis&gt;bicycle&lt;/graph:axis&gt;      &lt;graph:start&gt;0&lt;/graph:start&gt;      &lt;graph:end&gt;1000&lt;/graph:end&gt;      &lt;graph:interval&gt;50&lt;/graph:interval&gt;    &lt;/graph:dimension&gt;    &lt;graph:equation&gt;fish=0.01*bicycle+81.4&lt;/graph:equation&gt;  &lt;/graph:chart&gt;&lt;/report&gt;</pre></blockquote></div><p>Two namespaces are at play in this example. The first is the defaultnamespace, where elements and attributes lack a colon in their name.The elements whose names contain <tt class="literal">graph:</tt> are fromthe "chartml" namespace (somethingwe just made up). <tt class="literal">graph:</tt> is a <em class="emphasis">namespaceprefix</em><a name="INDEX-88" /> that, when attached to an element orattribute name, becomes a <em class="emphasis">qualifiedname</em><a name="INDEX-89" />. The two<tt class="literal">&lt;equation&gt;</tt> elements are completely differentelement types, with a different role to play in the document. The onein the default namespace is used to format an equation literally, andthe one in the chart namespace helps a graphing program generate acurve.</p><p>A namespace must always be declared in an element that contains theregion where it will be used. This is done with an attribute of theform<tt class="literal">xmlns:</tt><em class="replaceable">prefix</em>=<em class="replaceable">URL</em>,where <em class="replaceable">prefix</em> is the namespace prefix to beused (in this case, <tt class="literal">graph:</tt>) and<em class="replaceable">URL</em> is a unique identifier in the form ofa URL or other resource identifier. Outside of the scope of thiselement, the namespace is not recognized.</p><p>Besides keeping two like-named element types or attribute typesapart, namespaces serve a vital function in helping an XML processorformat a document. Sometimes the change in namespace indicates thatthe default formatter should be replaced with a kind that handles aspecific kind of data, such as the graph in the example. In othercases, a namespace is used to"bless" markup instructions to betreated as meta-markup, as in the case of XSLT.</p><p>Namespaces are emerging as a useful part of the XML tool set.However, they can raise a problem when DTDs are used. DTDs, as wewill explain later, may contain declarations that restrict the kindsof elements that can be used to finite sets. However, it can bedifficult to apply namespaces to DTDs, which have no special facilityfor resolving namespaces or knowing that elements and attributes thatfall under a namespace (beyond the ever-present default one) aredefined according to some other XML application.It's difficult to know this information partlybecause the notion of namespaces was added to XML long after theformat of DTDs, which have been around since the SGML days, was setin stone. Therefore, namespaces can be incompatible with some DTDs.This problem is still unresolved, though not because of any lack ofeffort in the standards community.</p><p><a href="ch10_01.htm">Chapter 10, "Coding Strategies"</a> covers some practical issues that emergewhen working<a name="INDEX-90" /> <a name="INDEX-91" /> with namespaces.</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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.2. Markup, Elements, and Structure</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.4. Spacing</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 &copy; 2002</a> O'Reilly &amp; 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 + -