XSD <anyAttribute>要素
<anyAttribute> 要素を使用すると、スキーマで規定されていない属性によって XML ドキュメントを拡張できます。
<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>
<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" 要素を拡張したいとします。この場合、スキーマの作成者が "gender" 属性を一切宣言していなくても、これを行うことができます。
このスキーマファイル "attribute.xsd" を見てください:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.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>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.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" という名前)は、異なるスキーマ "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.w3schools.com attribute.xsd">
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person gender="male">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
<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.w3schools.com attribute.xsd">
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person gender="male">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
上記の XML ファイルは有効です。これは、スキーマ "family.xsd" が "person" 要素に属性を追加することを許可しているためです。
<any> と <anyAttribute> はどちらも拡張可能なドキュメントを作成するために使用できます。これらにより、メインの XML スキーマで宣言されていない追加要素をドキュメントに含めることができます。
その他の拡張