📄 schema_example.asp
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML Schema Example</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="stylesheet" type="text/css" href="../stdtheme.css" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<a name="top"></a>
<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#808080">
<tr>
<td width="234" valign="top">
<a href="../default.htm"><img src="../images/w3default80.jpg" border="0" alt="W3Schools" /></a>
</td>
<th valign="middle" align="left" class="right">
<script type="text/javascript"><!--
google_ad_client = "pub-3440800076797949";
/*txtimg*/
google_ad_slot = "5330033957";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript" src="../../pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<noscript>
<a href="../default.asp" target="_blank"><img src="../banners/w6.gif" border="0" alt="W3Schools" /></a>
</noscript>
<br /></th>
</tr>
</table>
<table style="margin-left:1px" width="800px" border="0" cellpadding="0" cellspacing="2">
<tr>
<td width="137" class="content" valign="top"><br />
<a class="left" href="../default.asp" target="_top">
<img src="../images/homeicon.gif" border="0" alt="home" /></a>
<a class="left" href="../default.asp" target="_top">
<b>HOME</b></a>
<br /><br />
<b>Schema Tutorial</b><br />
<a class="left" target="_top" href="default.asp" >XSD HOME</a><br />
<a class="left" target="_top" href="schema_intro.asp" >XSD Intro</a><br />
<a class="left" target="_top" href="schema_why.asp" >XSD Why Use</a><br />
<a class="left" target="_top" href="schema_howto.asp" >XSD How To</a><br />
<a class="left" target="_top" href="schema_schema.asp" >XSD <schema></a><br />
<br />
<b>Simple Types</b><br />
<a class="left" target="_top" href="schema_simple.asp" >XSD Elements</a><br />
<a class="left" target="_top" href="schema_simple_attributes.asp" >XSD Attributes</a><br />
<a class="left" target="_top" href="schema_facets.asp" >XSD Restrictions</a><br />
<br />
<b>Complex Types</b><br />
<a class="left" target="_top" href="schema_complex.asp" >XSD Elements</a><br />
<a class="left" target="_top" href="schema_complex_empty.asp" >XSD Empty</a><br />
<a class="left" target="_top" href="schema_complex_elements.asp" >XSD Elements Only</a><br />
<a class="left" target="_top" href="schema_complex_text.asp" >XSD Text Only</a><br />
<a class="left" target="_top" href="schema_complex_mixed.asp" >XSD Mixed</a><br />
<a class="left" target="_top" href="schema_complex_indicators.asp" >XSD Indicators</a><br />
<a class="left" target="_top" href="schema_complex_any.asp" >XSD <any></a><br />
<a class="left" target="_top" href="schema_complex_anyattribute.asp" >XSD <anyAttribute></a><br />
<a class="left" target="_top" href="schema_complex_subst.asp" >XSD Substitution</a><br />
<a class="left" target="_top" href="schema_example.asp" style='font-weight:bold;'>XSD Example</a><br />
<br />
<b>Data Types</b><br />
<a class="left" target="_top" href="schema_dtypes_string.asp" >XSD String</a><br />
<a class="left" target="_top" href="schema_dtypes_date.asp" >XSD Date</a><br />
<a class="left" target="_top" href="schema_dtypes_numeric.asp" >XSD Numeric</a><br />
<a class="left" target="_top" href="schema_dtypes_misc.asp" >XSD Misc</a><br />
<br />
<a class="left" target="_top" href="schema_summary.asp" >XSD Summary</a><br />
<br />
<b>References</b><br />
<a class="left" target="_top" href="schema_elements_ref.asp" >XSD Reference</a><br />
<a class="left" target="_blank" href="../../www.w3.org/2001/03/webdata/xsv">XSD Validator</a><br /><br />
<b>Selected Reading</b><br />
<a class="left" target="_top" href="../browsers/browsers_stats.asp">Web Statistics</a><br />
<a class="left" target="_top" href="../site/site_glossary.asp">Web Glossary</a><br />
<a class="left" target="_top" href="../hosting/default.asp">Web Hosting</a><br />
<a class="left" target="_top" href="../quality/default.asp">Web Quality</a><br /><br />
<a class="left" target="_top" href="../forum/default.asp">W3Schools Forum</a><br /><br />
<a class="left" target="_top" href="../about/about_helping.asp">Helping W3Schools</a><br /><br />
<script type="text/javascript"><!--
google_ad_client = "pub-3440800076797949";
/*LeftLinkUnit*/
google_ad_slot = "4854527104";
google_ad_width = 120;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="../../pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
<td valign="top">
<table style="border: 1px solid gray" width="100%" bgcolor="#FFFFFF" border="0" cellpadding="5" cellspacing="0">
<tr>
<td>
<h1>An XSD Example</h1>
<a href="schema_complex_subst.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="schema_dtypes_string.asp"><img border="0" src="../images/btn_next.gif" alt="next" /></a>
<hr />
<p class="intro">This chapter will demonstrate how to write an XML Schema. You
will also learn that a schema can be written in different ways.</p>
<hr />
<h2>An XML Document</h2>
<p>Let's have a look at this XML document called "shiporder.xml":</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
<td>
<pre><?xml version="1.0" encoding="ISO-8859-1"?></pre>
<pre><shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder></pre>
</td>
</tr>
</table>
<p>The XML document above consists of a root element, "shiporder",
that contains a required attribute called "orderid". The "shiporder" element contains three
different child
elements: "orderperson", "shipto" and "item". The "item" element
appears twice, and it contains a "title", an optional "note" element, a "quantity", and a "price"
element. </p>
<p>The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
tells the XML parser that this document should be validated against a schema.
The line: xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE
the schema resides (here it is in the same folder as "shiporder.xml").</p>
<hr />
<h2>Create an XML Schema</h2>
<p>Now we want to create a schema for the XML document above.</p>
<p>We start by opening
a new file that we will call "shiporder.xsd". To create the schema we could simply follow
the structure in the XML document and define each element as we find it. We
will start with the standard XML declaration followed by the xs:schema element that defines a schema:</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
<td>
<pre><?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"></pre>
<pre>...</pre>
<pre>...
</xs:schema></pre>
</td>
</tr>
</table>
<p>In the schema above we use the standard namespace (xs), and the URI
associated with this namespace is the Schema language definition, which has the
standard value of http://www.w3.org/2001/XMLSchema. </p>
<p>Next, we have to define the "shiporder" element. This element has an
attribute and it contains other elements, therefore we consider it as a complex
type. The child elements of the "shiporder" element is surrounded by a
xs:sequence element that defines an ordered sequence of sub elements:</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
<td>
<pre><xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
...
</xs:sequence>
...
</xs:complexType>
</xs:element></pre>
</td>
</tr>
</table>
<p>Then we have to define the "orderperson" element as a simple type (because it does
not contain any attributes or other elements). The type (xs:string) is prefixed
with the namespace prefix associated with XML Schema that indicates a predefined
schema data type:</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
<td>
<pre><xs:element name="orderperson" type="xs:string"/></pre>
</td>
</tr>
</table>
<p>Next, we have to define two elements that are of the complex type: "shipto" and
"item". We start by defining the "shipto" element:</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
<td>
<pre><xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element></pre>
</td>
</tr>
</table>
<p>With schemas we can define the number of possible occurrences for an element
with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum
number of occurrences for an element and minOccurs specifies the minimum number
of occurrences for an element. The default value for both maxOccurs and
minOccurs is 1!</p>
<p>
Now we can define the "item" element. This element can appear multiple times
inside a "shiporder" element. This is specified by setting the maxOccurs
attribute of the "item" element to "unbounded" which means that there can be as
many occurrences of the "item" element as the author wishes. Notice that the
"note" element is optional. We have specified this by setting the minOccurs
attribute to zero:</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
<td>
<pre><xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element></pre>
</td>
</tr>
</table>
<p>We can now declare the attribute of the "shiporder" element. Since
this is a required attribute we specify use="required".</p>
<p><b>Note:</b> The attribute declarations must
always come last:</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
<td>
<pre><xs:attribute name="orderid" type="xs:string" use="required"/></pre>
</td>
</tr>
</table>
<p>
Here is the complete listing of the schema file called "shiporder.xsd":</p>
<table width="100%" border="1" class="ex" cellspacing="0">
<tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -