XSD 元素

來源:互聯網
上載者:User

XSD <any> XSD 元素替換

<anyAttribute> 元素使我們有能力通過未被 schema 規定的屬性來擴充 XML 文檔。 <anyAttribute> 元素

<anyAttribute> 元素使我們有能力通過未被 schema 規定的屬性來擴充 XML 文檔。

下面的例子是來自名為 "family.xsd" 的 XML schema 的一個片段。它為我們展示了針對 "person" 元素的一個聲明。通過使用 <anyAttribute> 元素,我們就可以向 "person" 元素添加任意數量的屬性:

<xs:element name="person">  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>    <xs:anyAttribute/>  </xs:complexType></xs:element>

現在,我們希望通過 "gender" 屬性來擴充 "person" 元素。在這種情況下我們就可以這樣做,即使這個 schema 的作者從未聲明過任何 "gender" 屬性。

請看這個 schema 檔案,名為 "attribute.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.w3school.com.cn"xmlns="http://www.w3school.com.cn"elementFormDefault="qualified"><xs:attribute name="gender">  <xs:simpleType>    <xs:restriction base="xs:string">      <xs:pattern value="male|female"/>    </xs:restriction>  </xs:simpleType></xs:attribute></xs:schema>

下面這個 XML(名為 "Myfamily.xml"),使用了來自不同 schema 的成分,"family.xsd" 和 "attribute.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns="http://www.microsoft.com"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:SchemaLocation="http://www.microsoft.com family.xsdhttp://www.w3school.com.cn attribute.xsd"><person gender="female"><firstname>Jane</firstname><lastname>Smith</lastname></person><person gender="male"><firstname>David</firstname><lastname>Smith</lastname></person></persons>

上面這個 XML 檔案是有效,這是因為 schema "family.xsd" 允許我們向 "person" 元素添加屬性。

<any> 和 <anyAttribute> 均可用於製作可擴充的文檔。它們使文檔有能力包含未在主 XML schema 中聲明過的附加元素。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.