ఒక XSD ఉదాహరణ

ఈ భాగంలో, మీరు XML Schema రాయడాన్ని చూపిస్తాము. మీరు స్కీమా రాయడానికి వివిధ పద్ధతులను కూడా నేర్చుకుంటారు.

XML డాక్యుమెంట్

దయచేసి, "shiporder.xml" అనే పేరుతో ఉన్న XML డాక్యుమెంట్ నిరీక్షించండి:

<?xml version="1.0" encoding="ISO-8859-1"?>



 <orderperson>George Bush</orderperson>
 <shipto>
  <name>John Adams</name>
  <address>Oxford Street</address>
  <city>London</city>
  <country>UK</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>

పైన ఈ XML డాక్యుమెంట్ "shiporder" రూపంలో ఉంది, దీనిలో "orderid" అనే నామమాత్రపు అంశం ఉంది. "shiporder" అంశం మూడు వివిధ ఉపాంశాలను కలిగి ఉంది: "orderperson"、"shipto" మరియు "item". "item" అంశం రెండు సార్లు కనిపిస్తుంది, దీనిలో "title" మరియు ఎక్కువగా లేని "note" అంశం, "quantity" మరియు "price" అంశం ఉన్నాయి.

పైన ఈ పద్ధతి xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" వాడబడుతుంది XML పరిశీలకంపై కొన్ని స్కీమా ఉపయోగించడానికి తెలుపుతుంది. ఈ పద్ధతి: xsi:noNamespaceSchemaLocation="shiporder.xsd" స్కీమా స్థానాన్ని నిర్ధారించుతుంది (ఇక్కడ, ఇది "shiporder.xml" తో అదే ఫోల్డర్ లో ఉంది).

创建一个 XML Schema

现在,我们需要为上面这个 XML 文档创建一个 schema。

我们可以通过打开一个新的文件来开始,并把这个文件命名为 "shiporder.xsd"。要创建schema,我们仅仅需要简单地遵循 XML 文档中的结构,定义我们所发现的每个元素。首先我们开始定义一个标准的 XML 声明:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
...

在上面的 schema 中,我们使用了标准的命名空间 (xs),与此命名空间相关联的 URI 是 Schema 的语言定义(Schema language definition),其标准值是 http://www.w3.org/2001/XMLSchema。

接下来,我们需要定义 "shiporder" 元素。此元素拥有一个属性,其中包含其他的元素,因此我们将它认定为复合类型。"shiporder" 元素的子元素被 xs:sequence 元素包围,定义了子元素的次序:

<xs:element name="shiporder">
 <xs:complexType>
  
  ...
  ...
  
  ...
 
</xs:element>

然后我们需要把 "orderperson" 元素定义为简易类型(这是因为它不包含任何属性或者其他的元素)。类型 (xs:string) 的前缀是由命名空间的前缀规定的,此命名空间与指示预定义的 schema 数据类型的 XML schema 相关联:

<xs:element name="orderperson" type="xs:string"/>

తరువాత, నేను రెండు ఎలిమెంట్లను కంపోజిట్ రకంగా నిర్వచించవలసివుంది: "shipto" మరియు "item". మేము "shipto" ఎలిమెంట్ను నిర్వచించడం మొదలు చేస్తాము:

<xs:element name="shipto">
 <xs:complexType>
  
   <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:element>

స్కీమా ద్వారా, మేము maxOccurs మరియు minOccurs అట్రిబ్యూట్లను ఉపయోగించి ఒక ఎలిమెంట్ కనిపించే సాధ్యతలను నిర్వచించవచ్చు. maxOccurs అనేది ఒక ఎలిమెంట్ కనిపించే గరిష్ట సంఖ్యను నిర్వచిస్తుంది, మరియు minOccurs అనేది ఒక ఎలిమెంట్ కనిపించే కనీస సంఖ్యను నిర్వచిస్తుంది. maxOccurs మరియు minOccurs యొక్క డిఫాల్ట్ విలువలు 1 ఉన్నాయి!

ఇప్పుడు, మేము "item" ఎలిమెంట్ను నిర్వచించవచ్చు. ఈ ఎలిమెంట్ "shiporder" ఎలిమెంట్లో అనేకసార్లు కనిపించవచ్చు. ఇది "item" ఎలిమెంట్యొక్క maxOccurs అట్రిబ్యూట్ విలువను "unbounded" గా సెట్ చేయడం ద్వారా సాధ్యపడుతుంది, అలా ఇది సృష్టికర్త కోరుకున్న ఏమైనా అనేకసార్లు కనిపించవచ్చు. దయచేసి, "note" ఎలిమెంట్ ఎంపికలుకొనిపోయింది. మేము ఈ ఎలిమెంట్యొక్క minOccurs అట్రిబ్యూట్ విలువను 0 గా సెట్ చేశాము:

<xs:element name="item" maxOccurs="unbounded">
 <xs:complexType>
  
   <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:element>

ఇప్పుడు, మేము "shiporder" అంశం యొక్క లక్షణాలను ప్రకటించవచ్చు. ఇది అనివార్య లక్షణం కాబట్టి, మేము use="required" నిర్ణయించాము.

ప్రతీక్షలు:ఈ లక్షణం ప్రకటన చివరికి ఉండాలి:

<xs:attribute name="orderid" type="xs:string" use="required"/>

ఈ "shiporder.xsd" పేరుతో ఉన్న schema ఫైల్ యొక్క డాక్యుమెంట్ జాబితా ఈ కింద ఉంది:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
 <xs:complexType>
  
   <xs:element name="orderperson" type="xs:string"/>
   <xs:element name="shipto">
    <xs:complexType>
     
      <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:element>
   <xs:element name="item" maxOccurs="unbounded">
    <xs:complexType>
     
      <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:element>
  
  <xs:attribute name="orderid" type="xs:string" use="required"/>
 
</xs:element>

Schema నిర్వహణ

ముంది డిజైనం చాలా సులభం, కానీ డాక్యుమెంట్ చాలా కష్టంగా చదవబడి, నిర్వహించబడి ఉంటుంది.

ముందుగా అన్ని అంశాలు మరియు లక్షణాలను నిర్వచించిన తరువాత, ref లక్షణం ద్వారా వాటిని ఉపయోగించే ఆధారపూర్వక డిజైనం ప్రస్తుతం ప్రస్తావించబడుతుంది.

ఈ నూతన పద్ధతిలో రూపొందించబడిన schema ఫైల్:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- 简易元素的定义 -->
<xs:element name="orderperson" type="xs:string"/>
<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:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- అంశం నిర్వచన -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- కమ్పోజిట్ ఎలిమెంట్ నిర్వచన -->
<xs:element name="shipto">
 <xs:complexType>
  
   <xs:element ref="name"/>
   <xs:element ref="address"/>
   <xs:element ref="city"/>
   <xs:element ref="country"/>
  
 
</xs:element>
<xs:element name="item">
 <xs:complexType>
  
   <xs:element ref="title"/>
   <xs:element ref="note" minOccurs="0"/>
   <xs:element ref="quantity"/>
   <xs:element ref="price"/>
  
 
</xs:element>
<xs:element name="shiporder">
 <xs:complexType>
  
   <xs:element ref="orderperson"/>
   <xs:element ref="shipto"/>
   <xs:element ref="item" maxOccurs="unbounded"/>
  
  <xs:attribute ref="orderid" use="required"/>
 
</xs:element>

పేరుబద్ధ రకాలను ఉపయోగించండి

మూడవ రూపకల్పన పద్ధతి క్లాసులు లేదా రకాలను నిర్వచిస్తుంది, దీనివల్ల మాకు ఎలిమెంట్ నిర్వచనాలను పునర్వినియోగించడానికి సామర్థ్యం ఉంటుంది. ఈ విధంగా చేయవచ్చు: ముందు సరళ ఎలిమెంట్లు మరియు కమ్పోజిట్ ఎలిమెంట్లకు పేరు పెట్టండి, అప్పుడు ఎలిమెంట్ యొక్క type అంశాన్ని వాటికి సూచిస్తాము.

ఇది మూడవ పద్ధతిలో రూపొందించబడిన schema ఫైల్ ("shiporder.xsd") ఉంది:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
 <xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
 <xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>

 
</xs:simpleType>
<xs:simpleType name="orderidtype">
 <xs:restriction base="xs:string">
  <xs:pattern value="[0-9]{6}"/>
 </xs:restriction>
</xs:simpleType>

 
  
  
  
  
 


 
  
  
  
  
 


 
  
  
  
 
 



restriction 元素显示出数据类型源自于 W3C XML Schema 命名空间的数据类型。因此,下面的片段也就意味着元素或属性的值必须是字符串类型的值:

<xs:restriction base="xs:string">

పరిమితి అంశం సాధారణంగా అంశాలపై పరిమితులను విధిస్తుంది. ముందుకు ఉన్న ఆ స్కీమా నుండి ఈ ఫ్రేగ్మెంట్స్ చూడండి:

<xs:simpleType name="orderidtype">
 <xs:restriction base="xs:string">
  <xs:pattern value="[0-9]{6}"/>
 </xs:restriction>
</xs:simpleType>

ఈ కోడు అంశం లేదా అంశం విలువను స్ట్రింగ్ అయించాలి, మరియు క్రమంగా ఆరు అక్షరాలు ఉండాలి, ఈ అక్షరాలు 0-9 సంఖ్యలు ఉండాలి.