XSD <anyAttribute> 要素
<anyAttribute>要素は、スキーマによって定義されていない属性を通じてXMLドキュメントを拡張する能力を私たちに与えます!
<anyAttribute>要素
<anyAttribute>要素は、スキーマによって定義されていない属性を通じてXMLドキュメントを拡張する能力を私たちに与えます!
以下の例は、"family.xsd"という名前のXMLスキーマから抜粋されたものです。これは"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.codew3c.com" xmlns="http://www.codew3c.com" 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.xsd" http://www.codew3c.com 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 で宣言されていない追加要素をドキュメントに含めることができます。