课程表

Schema 教程

简单的类型

复杂的类型

数据类型

参考手册

工具箱
速查手册

XSD <any>

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

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

<any> 元素

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

下面这个例子是从名为 "family.xsd" 的 XML schema 中引用的片段。它展示了一个针对 "person" 元素的声明。通过使用 <any> 元素,我们可以通过任何元素(在 <lastname> 之后)扩展 "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:any minOccurs="0"/>
  7. </xs:sequence>
  8. </xs:complexType>
  9. </xs:element>

现在,我们希望使用 "children" 元素来扩展 "person" 元素。这此种情况下我们就可以这么做,即使以上这个 schema 的作者没有声明任何 "children" 元素。

请看这个 schema 文件,名为 "children.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:element name="children">
  8. <xs:complexType>
  9. <xs:sequence>
  10. <xs:element name="childname" type="xs:string"
  11. maxOccurs="unbounded"/>
  12. </xs:sequence>
  13. </xs:complexType>
  14. </xs:element>
  15.  
  16. </xs:schema>

下面这个 XML 文件(名为 "Myfamily.xml"),使用了来自两个不同的 schema 中的成分,"family.xsd" 和 "children.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 children.xsd">
  7.  
  8. <person>
  9. <firstname>David</firstname>
  10. <lastname>Smith</lastname>
  11. <children>
  12. <childname>mike</childname>
  13. </children>
  14. </person>
  15.  
  16. <person>
  17. <firstname>Tony</firstname>
  18. <lastname>Smith</lastname>
  19. </person>
  20.  
  21. </persons>

上面这个 XML 文件是有效的,这是由于 schema "family.xsd" 允许我们通过在 "lastname" 元素后的可选元素来扩展 "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号