📄 jaxpdom8.html
字号:
<hr><a name="wp76890"> </a><p class="pBody">To contrive an example, consider an XML data set that keeps track of personnel data. The data set may include information from the w2 tax form, as well as information from the employee's hiring form, with both elements named <code class="cCode"><form></code> in their respective schemas. </p><a name="wp76891"> </a><p class="pBody">If a prefix is defined for the "tax" namespace, and another prefix defined for the "hiring" namespace, then the personnel data could include segments like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><employee id="..."> <name>....</name> <code class="cCodeBold"><tax:form></code> ...w2 tax form data... <code class="cCodeBold"></tax:form></code> <<code class="cCodeBold">hiring:form></code> ...employment history, etc.... <code class="cCodeBold"></hiring:form></code></employee><a name="wp76932"> </a></pre></div><a name="wp76698"> </a><p class="pBody">The contents of the <code class="cCode">tax:form</code> element would obviously be different from the contents of the <code class="cCode">hiring:form</code>, and would have to be validated differently. </p><a name="wp76964"> </a><p class="pBody">Note, too, that there is a "default" namespace in this example, that the unqualified element names <code class="cCode">employee</code> and <code class="cCode">name</code> belong to. For the document to be properly validated, the schema for that namespace must be declared, as well as the schemas for the <code class="cCode">tax</code> and <code class="cCode">hiring</code> namespaces.</p><hr><a name="wp77359"> </a><p class="pNote">Note: The "default" namespace is actually a <span style="font-style: italic">specific</span> namespace. It is defined as the "namespace that has no name". So you can't simply use one namespace as your default this week, and another namespace as the default later on. This "unnamed namespace" or "null namespace" is like the number zero. It doesn't have any value, to speak of (no name), but it is still precisely defined. So a namespace that does have a name can never be used as the "default" namespace.</p><hr><a name="wp77133"> </a><p class="pBody">When parsed, each element in the data set will be validated against the appropriate schema, as long as those schemas have been declared. Again, the schemas can either be declared as part of the XML data set, or in the program. (It is also possible to mix the declarations. In general, though, it is a good idea to keep all of the declarations together in one place.)</p><a name="wp77096"> </a><h4 class="pHeading3">Declaring the Schemas in the XML Data Set</h4><a name="wp76983"> </a><p class="pBody">To declare the schemas to use for the example above in the data set, the XML code would look something like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><<code class="cVariable">documentRoot</code> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <code class="cCodeBold">xsi:noNamespaceSchemaLocation</code>="<code class="cVariable">employeeDatabase</code>.xsd" <code class="cCodeBold">xsi:schemaLocation</code>= "http://www.irs.gov/ <code class="cVariable">fullpath</code>/w2TaxForm.xsd http://www.ourcompany.com/ <code class="cVariable">relpath</code>/hiringForm.xsd" <code class="cCodeBold">xmlns:tax</code>="http://www.irs.gov/" <code class="cCodeBold">xmlns:hiring</code>="http://www.ourcompany.com/"> ...<a name="wp77040"> </a></pre></div><a name="wp76987"> </a><p class="pBody">The <code class="cCode">noNamespaceSchemaLocation</code> declaration is something you've seen before, as are the last two entries, which define the namespace prefixes <code class="cCode">tax</code> and <code class="cCode">hiring</code>. What's new is the entry in the middle, which defines the locations of the schemas to use for each namespace referenced in the document.</p><a name="wp77090"> </a><p class="pBody">The <code class="cCode">xsi:schemaLocation</code> declaration consists of entry pairs, where the first entry in each pair is a fully qualified URI that specifies the namespace, and the second entry contains a full path or a relative path to the schema definition. (In general, fully qualified paths are recommended. That way, only one copy of the schema will tend to exist.)</p><a name="wp77089"> </a><p class="pBody">Of particular note is the fact that the namespace prefixes cannot be used when defining the schema locations. The <code class="cCode">xsi:schemaLocation</code> declaration only understands namespace names, not prefixes.</p><a name="wp77100"> </a><h4 class="pHeading3">Declaring the Schemas in the Application</h4><a name="wp77101"> </a><p class="pBody">To declare the equivalent schemas in the application, the code would look something like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">static final String <code class="cCodeBold">employeeSchema</code> = "employeeDatabase.xsd";static final String <code class="cCodeBold">taxSchema</code> = "w2TaxForm.xsd";static final String <code class="cCodeBold">hiringSchema</code> = "hiringForm.xsd";static final String[] <code class="cCodeBold">schemas = { employeeSchema, taxSchema, hiringSchema, }</code>;static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";...DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()...<code class="cCodeBold">factory.setAttribute</code>(JAXP_SCHEMA_SOURCE, <code class="cCodeBold">schemas</code>);<a name="wp77265"> </a></pre></div><a name="wp77113"> </a><p class="pBody">Here, the array of strings that points to the schema definitions (<code class="cCode">.xsd</code> files) is passed as the argument to factory.setAttribute method. Note the differences from when you were declaring the schemas to use as part of the XML data set:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp77423"> </a><div class="pSmartList1"><li>There is no special declaration for the "default" (unnamed) schema.</li></div><a name="wp77461"> </a><div class="pSmartList1"><li>You don't specify the namespace name. Instead, you only give pointers to the <code class="cCode">.xsd</code> files.</li></div></ul></div><a name="wp77462"> </a><p class="pBody">To make the namespace assignments, the parser reads the .xsd files, and finds in them the name of the <span style="font-style: italic">target namespace</span> they apply to. Since the files are specified with URIs, the parser can use an EntityResolver (if one has been defined) to find a local copy of the schema. </p><a name="wp77469"> </a><p class="pBody">If the schema definition does not define a target namespace, then it applies to the "default" (unnamed, or null) namespace. So, in the example above, you would expect to see these target namespace declarations in the schemas:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp77486"> </a><div class="pSmartList1"><li>employeeDatabase.xsd -- none</li></div><a name="wp77498"> </a><div class="pSmartList1"><li>w2TaxForm.xsd -- http://www.irs.gov/</li></div><a name="wp77503"> </a><div class="pSmartList1"><li>hiringForm.xsd -- http://www.ourcompany.com</li></div></ul></div><a name="wp77532"> </a><p class="pBody">At this point, you have seen two possible values for the schema source property when invoking the <code class="cCode">factory.setAttribute()</code> method, a File object in <code class="cCode">factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource))</code>. and an array of strings in <code class="cCode">factory.setAttribute(JAXP_SCHEMA_SOURCE, schemas)</code>. Here is a complete list of the possible values for that argument:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp77294"> </a><div class="pSmartList1"><li>String that points to the URI of the schema</li></div><a name="wp77295"> </a><div class="pSmartList1"><li>InputStream with the contents of the schema</li></div><a name="wp77296"> </a><div class="pSmartList1"><li>SAX InputSource</li></div><a name="wp77297"> </a><div class="pSmartList1"><li>File</li></div><a name="wp77298"> </a><div class="pSmartList1"><li>an array of Objects, each of which is one of the types defined above.</li></div></ul></div><hr><a name="wp77596"> </a><p class="pNote">Note: An array of Objects can be used only when the schema language (like <code class="cCode">http://java.sun.com/xml/jaxp/properties/schemaLanguage</code>) has the ability to assemble a schema at runtime. Also: When an array of Objects is passed it is illegal to have two schemas that share the same namespace.</p><hr> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JAXPDOM7.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPDOM9.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -