📄 lib0052.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Creating XML Document Formats</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0051.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0053.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch07"></a>
<div class="section">
<h2 class="first-section-title"><a name="243"></a><a name="ch07lev1sec5"></a>Creating XML Document Formats</h2><p class="first-para">In addition to their use in database design, data-modeling techniques can easily be applied to designing XML documents. The same data models that database administrators use to create physical database designs also readily translate into XML document formats, such as DTDs or schemas. XML is most often used as a means of communication between applications.</p>
<p class="para">The first step in creating any XML document is to identify the document root. XML documents usually contain lists of things identified in the data model. For instance, a <span class="fixed"><customer-update></span> document might contain a list of customer-related elements that contain information hat has changed. A <span class="fixed"><purchase-order></span> document might contain a list of order-related elements describing one or more purchase order contents.</p>
<p class="para">
<b class="bold">Entities in a data model translate to elements in an XML document.</b> Only implement the elements that are needed for the documents you're creating. Chances are that you don't need all entities translated into elements. Entities that represent small look-up value domains (e.g., <span class="fixed">CUSTOMER_TYPE</span>, <span class="fixed">ACCOUNT_TYPE</span>, etc.) are usually implemented as attributes rather than elements in an XML document.</p>
<p class="para">
<b class="bold">Attributes of an entity become attributes of the corresponding element. </b>For example, the <span class="fixed"><customer></span> element from <a href="LiB0048.html#222" target="_parent" class="chapterjump">figure 7.1</a> would have the attributes <span class="fixed">customer-id</span>, <span class="fixed">last-name</span>, <span class="fixed">first-name</span>, and <span class="fixed">telephone</span>.</p>
<p class="para">
<b class="bold">A one-to-many relationship implies that one element is the child of another in an XML document.</b> Unlike relational databases, a foreign key to the parent element isn't needed because it's indicated by segment ancestry. Ancestry is indicated naturally within the XML syntax. For example, the <a name="244"></a><a name="IDX-96"></a><span class="fixed"><customer></span> element from <a href="LiB0048.html#222" target="_parent" class="chapterjump">figure 7.1</a> would have an optional <span class="fixed"><account></span> child element.</p>
<p class="para">As a more complete illustration, <a class="internaljump" href="#ch07list02">listing 7.2</a> is a sample XML document for the data model in <a href="LiB0048.html#222" target="_parent" class="chapterjump">figure 7.1</a>.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 7.2: </span>XML Documnt Example</span><a name="245"></a><a name="ch07list02"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
<?xml version="1.0" encoding=" UTF-8"?>
<customer-update>
<customer customer-id=" C123"
first-name=" Derek"
last-name=" Ashmore"
telephone="999-990-9999">
<account account-id=" A1"
account-name=" Personal Checking"
account-type=" checking"/>
</customer>
</customer-update>
A sample DTD definition for this XML document type follows:
<?xml version="1.0" encoding=" UTF-8"?>
<!ELEMENT account EMPTY>
<!ATTLIST account
account-id CDATA #REQUIRED
account-name CDATA #REQUIRED
account-type CDATA #REQUIRED
>
<!ELEMENT customer (account)>
<!ATTLIST customer
customer-id CDATA #REQUIRED
first-name CDATA #REQUIRED
last-name CDATA #REQUIRED
telephone CDATA #REQUIRED
>
<!ELEMENT customer-update (customer)>
A sample Schema definition for this XML document type follows:
<?xml version="1.0" encoding=" UTF-8"?>
<!DOCTYPE xsd:schema PUBLIC "-//W3C//DTD XMLSCHEMA 19991216//EN" "" [
<!ENTITY % p 'xsd:'>
<!ENTITY % s ':xsd'>
]>
<xsd:schema xmlns:xsd=" http://www.w3.org/1999/XMLSchema">
<xsd:complexType name=" accountType"
content=" empty">
<xsd:attribute name=" account-id"
type=" xsd:string" use=" required"/>
<xsd:attribute name=" account-name"<a name="246"></a><a name="IDX-97"></a>
type=" xsd:string" use=" required"/>
<xsd:attribute name=" account-type"
type=" xsd:string" use=" required"/>
</xsd:complexType>
<xsd:complexType name=" customerType"
content=" elementOnly">
<xsd:sequence>
<xsd:element name=" account"
type=" accountType"/>
</xsd:sequence>
<xsd:attribute name=" customer-id"
use=" required">
<xsd:simpleType base=" xsd:binary">
<xsd:encoding value=" hex"/>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name=" first-name"
type=" xsd:string" use=" required"/>
<xsd:attribute name=" last-name"
type=" xsd:string" use=" required"/>
<xsd:attribute name=" telephone"
type=" xsd:string" use=" required"/>
</xsd:complexType>
<xsd:element name=" customer-update">
<xsd:complexType content=" elementOnly">
<xsd:sequence>
<xsd:element name=" customer"
type=" customerType"/>
</xsd:sequence>
<xsd:attribute name=" xmlns:xsi"
type=" xsd:uriReference"
use=" default"
value=" http://www.w3.org/1999/XMLSchema-instance"/>
<xsd:attribute
name=" xsi:noNamespaceSchemaLocation"
type=" xsd:string"/>
<xsd:attribute
name=" xsi:schemaLocation"
type=" xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<b class="bold">Rewrite all many-to-many relationships by choosing one of the entities of</b> <b class="bold">each relationship to be a child element.</b> For example, consider a many-to-many relationship between customer orders and products. This relationship would be rewritten as two one-to-many relationships using the entity <span class="fixed">ORDER_LINE_ITEM</span> as a cross-reference. An <span class="fixed"><order-line-item></span> element <a name="247"></a><a name="IDX-98"></a>could be a child of the <span class="fixed"><order></span> or <span class="fixed"><product></span> element, or both. Chances are that both do not need to be implemented and that <span class="fixed"><order-line-item></span> would be considered a child of <span class="fixed"><order></span>.</p>
<div class="section">
<h3 class="sect3-title">
<a name="248"></a><a name="ch07lev2sec2"></a>Common Mistakes</h3>
<p class="para">
<b>Declaring attributes as elements.</b> One of the most common XML design mistakes I see is making data elements that should be attributes. For example, some developers would have made <span class="fixed">account-name</span>, from <a class="internaljump" href="#ch07list02">listing 7.2</a>, a separate element instead of an attribute of <span class="fixed"><account></span>. Misusing elements in this way is likely to cause lower parsing performance and slower XSLT transformations.</p>
</div>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0051.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0053.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -