您现在的位置: 365建站网 > 建站教程 > Schema 教程 > XSD <anyAttribute> 元素

XSD <anyAttribute> 元素

此节有 330 人学习过     参与评论

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

<anyAttribute> 元素

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

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

</>code

  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":

</>code

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

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

</>code

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

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

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

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

您可能感兴趣的文章:

发表评论 (330人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------
Schema 教程目录

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号