课程表

Schema 教程

简单的类型

复杂的类型

数据类型

参考手册

工具箱
速查手册

XSD <anyAttribute>

当前位置:免费教程 » XML相关 » Schema

<anyAttribute> 元素使我们有能力通过未被 schema 规定的属性来扩展 XML 文档!

<anyAttribute> 元素

<anyAttribute> 元素使我们有能力通过未被 schema 规定的属性来扩展 XML 文档!

下面的例子是来自名为 "family.xsd" 的 XML schema 的一个片段。它为我们展示了针对 "person" 元素的一个声明。通过使用 <anyAttribute> 元素,我们就可以向 "person" 元素添加任意数量的属性:

  1. <xs:element name="person">
  2. <xs:complexType>
  3. <xs:sequence>
  4. <xs:element name="firstname" type="xs:string"/>
  5. <xs:element name="lastname" type="xs:string"/>
  6. </xs:sequence>
  7. <xs:anyAttribute/>
  8. </xs:complexType>
  9. </xs:element>

现在,我们希望通过 "gender" 属性来扩展 "person" 元素。在这种情况下我们就可以这样做,即使这个 schema 的作者从未声明过任何 "gender" 属性。

请看这个 schema 文件,名为 "attribute.xsd":

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="//www.w3xue.com"
  4. xmlns="//www.w3xue.com"
  5. elementFormDefault="qualified">
  6.  
  7. <xs:attribute name="gender">
  8. <xs:simpleType>
  9. <xs:restriction base="xs:string">
  10. <xs:pattern value="male|female"/>
  11. </xs:restriction>
  12. </xs:simpleType>
  13. </xs:attribute>
  14.  
  15. </xs:schema>

下面这个 XML(名为 "Myfamily.xml"),使用了来自不同 schema 的成分,"family.xsd" 和 "attribute.xsd":

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <persons xmlns="http://www.microsoft.com"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:SchemaLocation="http://www.microsoft.com family.xsd
  6. //www.w3xue.com attribute.xsd">
  7.  
  8. <person gender="female">
  9. <firstname>Jane</firstname>
  10. <lastname>Smith</lastname>
  11. </person>
  12.  
  13. <person gender="male">
  14. <firstname>David</firstname>
  15. <lastname>Smith</lastname>
  16. </person>
  17.  
  18. </persons>

上面这个 XML 文件是有效的,这是因为 schema "family.xsd" 允许我们向 "person" 元素添加属性。

<any> 和 <anyAttribute> 均可用于制作可扩展的文档!它们使文档有能力包含未在主 XML schema 中声明过的附加元素。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号