经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » XML相关 » Schema » 查看文章
璇﹁ВXML涓殑妯″紡Schema_XML/XSLT
来源:jb51  时间:2021/12/8 8:39:28  对本文有异议

鏂囨。瀹氫箟妯″瀷涓篨ML鏂囨。鎻愪緵浜嗚鑼冿紝DTD鐨勫紩鍏ヨ櫧鐒惰В鍐充簡XML鏂囨。鐨勮鑼冨寲闂锛屼絾瀹冩枃浠舵牸寮忕被鍨嬪拰XML鏂囦欢鏍煎紡绫诲瀷涓嶄竴鑷达紝鍚屾椂DTD鎻愪緵鐨勬暟鎹被鍨嬫湁闄愭湁鏃跺€欐棤娉曟弧瓒宠涓氱殑闇€姹傦紝鎵€浠ュ紩鍏ヤ簡Schema銆係chema妯″紡鍙互纭畾XML鏂囨。鐨勫厓绱犲拰灞炴€х殑缁撴瀯銆佸厓绱犵殑椤哄簭銆佸厓绱犲拰灞炴€х殑鏁版嵁鍊硷紝鏍规嵁鑼冨洿銆佹灇涓句互鍙婃牱寮忓尮閰嶇瓑銆?/p>

2016624152803453.png (467×310)

涓€銆丼chema鍒濊瘑

XML Schema璇█涔熻绉颁负XML Schema Definition (XSD)锛屽畠鐨勪綔鐢ㄦ槸瀹氫箟涓€浠絏ML鏂囨。鐨勫悎娉曠粍浠剁兢锛圶ML鏂囨。鐨勭粨鏋勶級锛屽氨鍍廌TD鐨勪綔鐢ㄤ竴鏍枫€?XML Schema浠ML璇█浣滀负鍩虹锛屼篃鍙互璇碭ML Schema鑷韩灏辨槸XML鐨勪竴绉嶅簲鐢ㄣ€?/p>

1銆丼chema浣滅敤

XML Schema鍜孌TD鐨勪綔鐢ㄤ竴鏍凤紝閮芥槸鐢ㄦ潵瀹氫箟涓€涓猉ML鏂囨。鐨勭粨鏋勭殑妯″紡锛岄偅涔堜负浠€涔堟湁浜咲TD杩樿鏈塜ML Schema鍛紵 鍥犱负锛孹ML Schema姣擠TD浣滅敤鏇村姞寮哄ぇ銆?/p>

Xml Schema 姣?DTD鐨勪紭瓒婃€э細
锛?锛夋ā寮忔槸鍙墿灞曠殑
锛?锛夋ā寮忔瘮 DTD 鍔熻兘鏇翠赴瀵屼笖鏇存湁鐢?br /> 锛?锛夋ā寮忔槸鐢?XML 缂栧啓鐨?br /> 锛?锛夋ā寮忔敮鎸佹暟鎹被鍨?br /> 锛?锛夋ā寮忔敮鎸佸悕绉扮┖闂?br /> 锛?锛変笉闇€瑕佸湪瀛︿範鍏朵粬鐨勮瑷€
锛?锛夊彲浠ョ洿鎺ヤ娇鐢╔ML缂栬緫鍣ㄦ潵缂栧啓XML Schema
锛?锛夊彲浠ョ洿鎺ヤ娇鐢╔ML瑙f瀽鍣ㄦ潵瑙f瀽XML Schema
锛?锛夊彲浠ヤ娇鐢╔ML DOM鏉ョ伒娲荤殑鎿嶄綔XML Schema
锛?0锛夊彲浠ヤ娇鐢╔SLT鎶€鏈潵杞崲XML Schema

2銆佸姣斿涔?/h3>

1.1 浣滅敤涓?/strong>

涓よ€呯浉鍚岋紝閮芥槸瀹氫箟浜嗕竴涓猉ML鏂囨。鐨勭粨鏋勭殑妯″紡銆?/p>

1.2 鐢ㄦ硶涓?/strong>

涓よ€呬綔鐢ㄧ浉鍚岋紝浣嗘槸璇硶涓婃湁浜涘尯鍒€係chema鏄疍TD鐨勬墿灞曪紝鍚屾牱鏀寔鍏冪礌鍜屽睘鎬х殑瀹氫箟锛屽苟涓斿畾涔夌殑璇硶鐩镐技锛屼絾Schema鍙互缁欏厓绱犲拰灞炴€ф坊鍔犵浉搴旂殑鏁版嵁绫诲瀷锛屽悓鏃惰繕寮曞叆浜嗗叏灞€鍜屽眬閮ㄥ厓绱犲0鏄庣殑璇硶锛屽彟澶栨牴鎹厓绱犲拰灞炴€х殑鏁版嵁鍐呭寮曞叆浜嗙畝鍗曠被鍨嬪拰澶嶆潅绫诲瀷銆?br /> 鎵€璋撶殑绠€鍗曠被鍨?SimpleType)鍜屽鏉傜被鍨?ComplexType)锛屽畠浠湰韬苟涓嶆槸鍏蜂綋鐨勬暟鎹被鍨嬶紝瀹冧滑鍙槸瀵硅妭鐐规垨鑰呰嚜瀹氫箟绫诲瀷鐨勭被鍨嬩綔涓€涓娊璞$殑璇存槑銆?/p>

涔熷氨鏄Schema鐨勫紩鍏ヤ娇寰楁ā寮忕殑澹版槑鏇村姞绫讳技浜庢垜浠娇鐢ㄧ殑缂栫▼璇█銆?/p>

浜屻€佸疄渚嬭瑙?/h2>

2.1 Schema瀹炰緥

娓呭崟1锛歎ser.xml鏂囨。缁撴瀯

<?xml version="1.0"锛?gt;      
<鐢ㄦ埛鍒楄〃>      
    <鐢ㄦ埛>      
         <鐢ㄦ埛鍚?gt;xx</鐢ㄦ埛鍚?gt;      
         <瀵嗙爜>123456</瀵嗙爜>      
         <鐢ㄦ埛绫诲瀷>1</鐢ㄦ埛绫诲瀷>      
   </鐢ㄦ埛>      
</鐢ㄦ埛鍒楄〃>  

娓呭崟2锛氫娇鐢ㄥ叏灞€缁勪欢褰㈠紡鏉ュ畾涔塖chema锛孶ser.xsd

<!-- 浣跨敤鍏ㄥ眬缁勪欢褰㈠紡瀹氫箟 -->      
<?xml version='1.0' encoding='utf-8'?>      
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefualt='unqualified'>      
 <xs:element name='鐢ㄦ埛鍒楄〃' type='userlist'/>      
 <xs:complexType name='userlist'><!-- 浣跨敤complexType澹版槑璇ョ被鍨嬩负澶嶅悎绫诲瀷鐨勫厓绱?-->      
  <xs:sequence><!-- 浣跨敤sequence璇存槑涓嬮潰鐨勫厓绱犲繀椤绘寜椤哄簭鍦╔ML鏂囨。涓樉绀?-->      
   <xs:element name='鐢ㄦ埛' type='user'/>      
  </xs:sequence>      
 </xs:complexType>      
 <xs:complexType name='user'>      
  <xs:sequence>      
   <xs:element name='鐢ㄦ埛鍚? type='user'/>      
   <xs:element name='瀵嗙爜' type='user'/>      
   <xs:element name='鐢ㄦ埛绫诲瀷' type='user'/>      
  </xs:sequence>      
 </xs:complexType>      
</xs:schema>

娓呭崟3锛氫娇鐢ㄥ眬閮ㄥ舰寮忓畾涔塖chema锛孶ser.xsd

<!-- 浣跨敤灞€閮ㄥ舰寮忓畾涔?-->      
<?xml version='1.0' encoding='utf-8'?>      
<xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'>      
 <xs:element name='鐢ㄦ埛鍒楄〃'>      
  <xs:complexType>      
   <xs:sequence>      
    <xs:element name='鐢ㄦ埛'>      
     <xs:complexType>      
      <xs:sequence>      
       <xs:element name='鐢ㄦ埛鍚? type='xs:string'/>      
       <xs:element name='瀵嗙爜' type='xs:string'/>      
       <xs:element name='鐢ㄦ埛绫诲瀷' type='xs:integer'/>      
      </xs:sequence>      
     </xs:complexType>      
    </xs:element>      
   </xs:sequence>      
  </xs:complexType>      
 </xs:element>      
</xs:schema> 

娓呭崟2鍜屾竻鍗?鐨勯兘瀹氫箟浜哢ser.xsd锛屽畠浠殑浣滅敤鏄浉鍚岀殑锛岄兘鏄负浜嗛獙璇乆ML鏂囨。鐨勫悎娉曟€э紝浣嗗畾涔夌殑鏂规硶涓嶅悓锛屾竻鍗?鏄娇鐢ㄥ叏灞€缁勪欢鐨勫舰寮忓畾涔夛紝鑰屾竻鍗?浣跨敤鐨勫眬閮ㄥ舰寮忓畾涔夛紝鍏蜂綋鐨勫尯鍒鐪嬩笅鏂囥€?/p>

2.2 鍩烘湰鐢ㄦ硶

涓婃枃涓垜浠垎鍒粠浣滅敤鍜岀敤娉曚笂瀵筍chema鍜孌TD杩涜浜嗗姣斿垎鏋愶紝Schema鍜孌TD鏈€澶х殑鍖哄埆鍦ㄤ簬Schema寮曞叆浜嗘暟鎹被鍨嬶紝鍏跺畠鐨勫鍏冪礌鍜屽睘鎬х殑澹版槑绛夌被浼间簬DTD锛屽湪涓嬮潰璁ㄨ鏃跺氨涓嶅啀璇﹁堪銆?/p>

Schema鍩烘湰鍐呭瀵煎浘锛?/p>

2016624153242570.png (530×744)

2.2.1 寮曠敤璇硶
褰撲竴涓ā寮忔枃浠跺缓绔嬪ソ浠ュ悗锛屽彲浠ョ敤瀹冩潵楠岃瘉鏌愪竴涓猉ML鏂囨。鐨勬湁鏁堟€э紝涔熷氨鏄妫€楠屾煇涓猉ML鏂囨。鏄惁閬靛惊浜嗘ā寮忔枃浠剁殑瀹氫箟銆?閭d箞锛孹ML鏂囨。鎬庝箞鏉ュ紩鐢ㄤ竴涓ā寮忔枃妗e憿锛?
Schema妯″瀷鐨勫紩鐢ㄦ洿绫讳技浜庡墠闈㈡墍璇寸殑鍛藉悕绌洪棿鐨勫簲鐢ㄦ柟娉曪紝鍏蜂綋瀹炰緥濡備笅锛?/p>

<?xml version='1.0' encoding='utf-8'?>      
<鐢ㄦ埛鍒楄〃 xmlns:xsi=http://www.nishishui.org/2000/XMLSchema xsi:noNamespaceSchemaLocation='user.xsd'><!-- 寮曠敤user.xsd -->      
 <鐢ㄦ埛>      
  <鐢ㄦ埛鍚?gt;鎴戞槸璋?lt;/鐢ㄦ埛鍚?gt;      
  <瀵嗙爜>123456</瀵嗙爜>      
  <鐢ㄦ埛绫诲瀷>1</鐢ㄦ埛绫诲瀷>      
 </鐢ㄦ埛>      
</鐢ㄦ埛鍒楄〃>

2.2.2 鍏冪礌绫诲瀷

锛?锛夋寜鐓у唴瀹圭殑涓嶅悓鍒嗕负绠€鍗曞拰澶嶆潅鍏冪礌锛屽垎鍒娇鐢╯impleType鍜宑omplexType鏍囩ず銆?/p>

绠€鍗曞厓绱狅細鍏冪礌涓唴瀹瑰彧鑳芥槸鏂囨湰锛屼笉鍖呭惈鍏跺畠鐨勫厓绱犲拰灞炴€с€?/p>

<?xml version='1.0' encoding='utf-8'?>      
<xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'>      
    <xs:element name='age'>      
        <xs:simpleType><!-- 浣跨敤鍏抽敭瀛梥impleType澹版槑绠€鍗曞厓绱?-->      
            <!--restriction鍏抽敭瀛楃粨鍚坢inInclusive鍜宮axInclusive鎺у埗浜哫ML涓厓绱犲彲鎺ュ彈鐨勫€艰寖鍥翠负0~100-->      
            <xs:restriction base="xs:integer">      
                <xs:minInclusive value='0'/>      
                <xs:maxInclusive value='100'/>      
            </xs:restriction>      
        </xs:simpleType>      
    </xs:element>      
</xs:schema>

澶嶆潅鍏冪礌锛氬厓绱犱腑鍖呭惈鍏跺畠鐨勫厓绱犲拰灞炴€с€傚畠鏈夊洓绉嶇被鍨嬶紝鍒嗗埆鏄┖鍏冪礌銆佸彧鍖呭惈鍏朵粬鍏冪礌銆佸彧鍖呭惈姝f枃銆佸寘鍚鏂囧張鍖呭惈鍏朵粬鍏冪礌銆?/p>

<?xml version='1.0' encoding='utf-8'?>      
<xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'>      
    <xs:element name='age'>      
        <xs:complexType><!-- 浣跨敤鍏抽敭瀛梒omplexType澹版槑澶嶆潅鍏冪礌 -->      
            <!--sequence鎺у埗XML鍐呭涓厓绱犲嚭鐜扮殑椤哄簭-->      
            <xs:sequence>      
                <!-- 瀹氫箟鍏蜂綋鐨勫厓绱狅紝杩欎簺閮芥槸绠€鍗曞厓绱?->      
                <xs:element name='firstname' type='xs:string'/>      
                <xs:element name='lastname' type='xs:string'/>      
            </xs:sequence>      
        </xs:complexType>      
    </xs:element>      
</xs:schema>

锛?锛夋寜鐓у畾涔変綅缃彲鍒嗕负灞€閮ㄥ拰鍏ㄥ眬鍏冪礌銆?br /> 鍏ㄥ眬鍏冪礌锛?lt;element>鍏冪礌鐨勭埗鍏冪礌蹇呴』鏄?lt;schema>锛?br /> 灞€閮ㄥ厓绱狅細灞€閮ㄥ厓绱犲0鏄庡彧鑳藉嚭鐜板湪澶嶆潅绫诲瀷锛?lt;complexType>鍏冪礌锛夊畾涔夊唴閮ㄣ€傚嵆<element>鍏冪礌鐨勭埗鍏冪礌鍙兘鏄?lt;all>銆?lt;choice>鎴?lt;sequence>鍏冪礌銆?/p>

<?xml version='1.0' encoding='utf-8'?>      
<xs:schema xmlns:xs='http://www.nishishui.org/2000/XML Schema' elementFormDefault='qualified' attributeFormDefualt='unqualified'>      
    <xs:element name='鐢ㄦ埛' type='user'/><!-- 鍏ㄥ眬鍏冪礌 -->      
    <xs:element name='鐢ㄦ埛鍚? type='xs:string'/><!-- 鍏ㄥ眬鍏冪礌 -->      
    <xs:element name='瀵嗙爜' type='xs:string'><!-- 鍏ㄥ眬鍏冪礌 -->      
        <xs:complexType name='user'>      
            <!--sequence鎺у埗XML鍐呭涓厓绱犲嚭鐜扮殑椤哄簭-->      
            <xs:sequence>      
                <!-- 瀹氫箟鍏蜂綋鐨勫厓绱狅紝杩欎簺閮芥槸绠€鍗曞厓绱?->      
                <!-- 瀹氫箟灞€閮ㄥ厓绱狅紝浣跨敤ref鍏抽敭瀛楀紩鐢紝骞朵娇鐢╩inOccurs鍜宮axOccurs鍒跺畾鍏冪礌鍑虹幇鐨勬渶灏戝拰鏈€澶氱殑娆℃暟-->      
                <xs:element ref='鐢ㄦ埛鍚? minOccurs='0' maxOccurs='1'/><!-- 灞€閮ㄥ厓绱?->      
                <xs:element ref='瀵嗙爜' minOccurs='0' maxOccurs='1'/><!-- 灞€閮ㄥ厓绱?->      
            </xs:sequence>      
        </xs:complexType>      
    </xs:element>      
</xs:schema>

鍒版杩欑瘒鍏充簬璇﹁ВXML涓殑妯″紡Schema鐨勬枃绔犲氨浠嬬粛鍒拌繖浜?鏇村鐩稿叧XML Schema鍐呭璇锋悳绱㈣剼鏈箣瀹朵互鍓嶇殑鏂囩珷鎴栫户缁祻瑙堜笅闈㈢殑鐩稿叧鏂囩珷锛屽笇鏈涘ぇ瀹朵互鍚庡澶氭敮鎸佽剼鏈箣瀹讹紒

 友情链接: NPS