<xs:anyAttribute>
元素用于扩展XSD功能。 它用于通过未在模式中定义的属性扩展在一个xsd中定义的complexType
元素。
下面来看看一个例子 - person.xsd 中定义了 person complexType
元素。 attributes.xsd 中定义了age
属性。
文件:person.xsd 的内容如下 -
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.yiibai.com"
xmlns = "http://www.yiibai.com"
elementFormDefault = "qualified">
<xs:element name = "person">
<xs:complexType >
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "lastname" type = "xs:string"/>
<xs:element name = "nickname" type = "xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
文件:attributes.xsd -
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.yiibai.com"
xmlns = "http://www.yiibai.com"
elementFormDefault = "qualified">
<xs:attribute name = "age">
<xs:simpleType>
<xs:restriction base = "xs:integer">
<xs:pattern value = "[0-100]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>
如果想要用XML定义person
元素的age
属性,则以下声明将无效。文件:person.xml -
<?xml version = "1.0"?>
<class xmlns = "http://www.yiibai.com"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.yiibai.com person.xsd
http://www.yiibai.com attributes.xsd">
<person age = "20">
<firstname>Max</firstname>
<lastname>Su</lastname>
<nickname>Maxsu</nickname>
</person>
</class>
使用
要验证上面的person.xml,请将<xs:anyAttribute>
添加到person.xsd 文件中的person
元素中。如下所示 -
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.yiibai.com"
xmlns = "http://www.yiibai.com"
elementFormDefault = "qualified">
<xs:element name = "person">
<xs:complexType >
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "lastname" type = "xs:string"/>
<xs:element name = "nickname" type = "xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:schema>
现在,文件:person.xml 将按person.xsd 和attributes.xsd 给出的定义进行验证。
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。
- Java技术群: 227270512 (人数:3000,免费:否)
- Go开发者群(新): 851549018 (人数:1000,免费)
- PHP开发者群: 460153241 (人数:2000,免费)
- MySQL/SQL群: 418407075 (人数:2000,免费:否)
- 大数据开发群: 655154550 (人数:2000,免费:否)
- Python技术群: 287904175 (人数:2000,免费:否)
- 人工智能深度学习: 456236082 (人数:2000,免费:否)
- 测试工程师群: 415553199 (人数:2000,免费:否)
- 前端开发者群: 410430016 (人数:2000,免费:否)
- C/C++技术群(新): 629264796 (人数:2000,免费)
- Node.js技术群(新): 621549808 (人数:2000,免费)
- PostgreSQL数据库群: 539504187 (人数:1000,免费)
- Linux运维技术群: 479429477 (人数:2000,免费:否)
- Oracle数据库: 175248146 (人数:2000,免费:否)
- C#/ASP.Net开发者: 579821706 (人数:2000,免费)
- 数据分析师群: 397883996 (人数:2000,免费:否)