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

📄 6_ns.html

📁 XML_JAVA指南 书籍语言: 简体中文 书籍类型: 程序设计 授权方式: 免费软件 书籍大小: 377 KB
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><!--NewPage--><html><head><title>Namespaces</title><style type="text/css"><!----></style></head><body BGCOLOR="#ffffff"><table width="100%"><tr>    <td align=left> <a href="5_create.html"><img src="../images/PreviousArrow.gif" width=25 height=26 align=top border=0 alt="Previous | "></a><ahref="index.html"><img src="../images/NextArrow.gif" width=26 height=26 align=top border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=top border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=top border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=top 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><h2>6. Using Namespaces</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>Local Links</i></b></dt>      </dl>      <ul>        <li><a href="../sax/5c_dtd.html#attrRefs">Defining Attributes in the DTD</a>         </li>      </ul>      <dl>         <dt><b><i>External Links</i></b></dt>      </dl>      <ul>        <li><a href="http://www.w3.org/TR/REC-xml-names/">Namespace Specification</a></li>      </ul>      <p><b><i>Glossary Terms</i></b></p>      <dl>         <dd> <a href="../glossary.html#attribute">attribute</a>, <a href="../glossary.html#namespace">namespace</a>,           <a href="../glossary.html#URI">URI</a>, <a href="../glossary.html#URL">URL</a>,           <a href="../glossary.html#URN">URN</a> <br>        </dd>      </dl>      </td>  </tr></table><p>As you saw previously, one way or another it is necessary to resolve the conflict   between the <code>title</code> element defined in <b>slideshow.dtd</b> and the   one defined in <b>xhtml.dtd</b>. In the previous exercise, you hyphenated the   name in order to put it into a different &quot;namespace&quot;. In this section,   you'll see how to use the XML <a href="../glossary.html#namespace">namespace</a>   standard to do the same thing without renaming the element. <blockquote>   <p><b>Note: </b>At this point in time, the Java XML parsers do not support namespaces.     This section is for information only.</p></blockquote><p>The primary goal of the namespace specification is to let the document author   tell the parser which DTD to use when parsing a given element. The parser can   then consult the appropriate DTD for an element definition. Of course, it is   also important to keep the parser from aborting when a &quot;duplicate&quot;   definition is found, and yet still generate an error if the document references   an element like <code>title</code> without <i>qualifying</i> it (identifying   the DTD to use for the definition). </p><blockquote>   <p><b>Note: </b><br>    Namespaces apply to attributes as well as to elements. In this section, we     consider only elements. For more information on attributes, consult the namespace     specification at <a href="http://www.w3.org/TR/REC-xml-names/">http://www.w3.org/TR/REC-xml-names/</a>.</p></blockquote><h3><a name="defining"></a>Defining a Namespace</h3><p>To define a namespace that an element belongs to, it is necessary to add an   <a href="../glossary.html#attribute">attribute</a> to the element's definition,   where the attribute name is <code>xmlns</code> (&quot;<u>xml</u> <u>n</u>ame<u>s</u>pace&quot;).   For example, you could do that in <b>slideshow.dtd</b> by adding an entry like   the following in the <code>title</code> element's attribute-list definition: <blockquote>   <pre>&lt;!ELEMENT title (%inline;)*><b>&lt;!ATTLIST title </b>   <b>xmlns CDATA #FIXED "http://www.example.com/slideshow"</b><b>&gt;</b></pre></blockquote><p>Declaring the attribute as <code>FIXED</code> has several important features: <ul>  <li>     <p>It prevents the document from specifying any non-matching value for the       <code>xmlns</code> attribute (as described in <a href="../sax/5c_dtd.html#attrRefs">Defining       Attributes in the DTD</a>).</p>  </li>  <li>     <p>The element defined in this DTD is made unique (because the parser understands       the <code>xmlns</code> attribute), so it does not conflict with an element       that has the same name in another DTD. That allows multiple DTDs to use       the same element name without generating a parser error.</p>  </li>  <li>     <p>When a document specifies the <code>xmlns</code> attribute for a tag, the       document selects the element definition with a matching attribute.</p>  </li></ul><p>To be thorough, every element name in your DTD would get the exact same attribute,   with the same value. (Here, though, we're only concerned about the <code>title</code>   element.) Note, too, that you are using a <code>CDATA</code> string to supply   the <a href="../glossary.html#URI">URI</a>. In this case, we've specified an   <a href="../glossary.html#URL">URL</a>. But you could also specify a <a href="../glossary.html#URN">URN</a>   , possibly by specifying a prefix like <code>urn:</code> instead of <code>http:</code>.   (URNs are currently being researched. They're not seeing a lot of action at   the moment, but that could change in the future.) <h3><a name="referencing"></a>Referencing a Namespace</h3><p>When a document uses an element name that exists in only one of the <code>.dtd</code>   files it references, the name does not need to be qualified. But when an element   name that has multiple definitions is used, some sort of qualification is a   necessity.</p><blockquote>   <p><b>Note: </b><br>    In point of fact, an element name is always qualified by it's <i>default namespace</i>,     as defined by name of the DTD file it resides in. As long as there as is only     one definition for the name, the qualification is implicit.</p></blockquote><p>You qualify a reference to an element name by specifying the xmlns attribute,   as shown here:</p><blockquote>   <pre>&lt;title <b>xmlns=&quot;http://www.example.com/slideshow&quot;</b>    Overview&lt;/title></pre></blockquote><p>The specified namespace applies to that element, and to any elements contained   within it.</p><p> <h3><a name="prefix"></a>Defining a Namespace Prefix</h3><p>When you only need one namespace reference, it's not such a big deal. But when   you need to make the same reference several times, adding <code>xmlns</code>   attributes becomes unwieldy. It also makes it harder to change the name of the   namespace at a later date.</p><p>The alternative is to define a <i>namespace prefix</i>, which as simple as   specifying xmlns, a colon (:) and the prefix name before the attribute value,   as shown here:</p><blockquote>   <pre>&lt;sl:slideshow xmlns<b>:sl</b>='http:/www.example.com/slideshow'              ...>    ...&lt;/sl:slideshow></pre></blockquote><p>This definition sets up <code>sl</code> as a prefix that can be used to qualify   the current element name and any element within it. Since the prefix can be   used on any of the contained elements, it makes the most sense to define it   on the XML document's root element, as shown here. </p><blockquote>   <p><b>Note: </b><br>    The namespace URI can contain characters which are not valid in an XML name,     so it cannot be used as a prefix directly. The prefix definition associates     an XML name with the URI, which allows the prefix name to be used instead.     It also makes it easier to change references to the URI in the future. </p></blockquote><p>When the prefix is used to qualify an element name, the end-tag also includes   the prefix, as highlighted here:</p><blockquote>   <pre>&lt;<b>sl:</b>slideshow xmlns:sl='http:/www.example.com/slideshow'              ...>    ...    &lt;slide&gt;       &lt;<b>sl:title</b>&gt;Overview&lt;<b>sl:title</b>&gt;    &lt;/slide&gt;    ...&lt;/<b>sl:</b>slideshow></pre></blockquote><blockquote> </blockquote><p>Finally, note that multiple prefixes can be defined in the same element, as   shown here:</p><blockquote></blockquote><blockquote>   <pre>&lt;sl:slideshow <b>xmlns:sl</b>='http:/www.example.com/slideshow'              <b>xmlns:xhtml='urn:...'</b>>    ... &lt;/sl:slideshow></pre></blockquote><p> With this kind of arrangement, all of the prefix definitions are together   in one place, and you can use them anywhere they are needed in the document.   This example also suggests the use of URN to define the <code>xhtml</code> prefix,   instead of an URL. That definition would conceivably allow the app to reference   a local copy of the XHTML DTD or some mirrored version, with a potentially beneficial   impact on performance.. </p><blockquote><hr size=4></blockquote><p><p> <table width="100%"><tr>    <td align=left> <a href="5_create.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=top border=0 alt="Previous | "></a><ahref="index.html"><img src="../images/NextArrow.gif" width=26 height=26 align=top border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=top border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=top border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=top 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></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -