XSD <any>要素
<any> 要素を使用すると、スキーマで規定されていない要素によって XML ドキュメントを拡張できます!
<any> 要素
<any> 要素を使用すると、スキーマで規定されていない要素によって XML ドキュメントを拡張できます!
次の例は、"family.xsd" という名前の XML スキーマから引用した断片です。これは "person" 要素に対する宣言を示しています。<any> 要素を使用することで、任意の要素(<lastname> の後)を使って "person" の内容を拡張できます:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
さて、"person" 要素を "children" 要素で拡張したいとします。この場合、上記のスキーマの作者が "children" 要素を一切宣言していなくても、これを行うことができます。
次のスキーマファイル "children.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:element name="children">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</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:element name="children">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
次の XML ファイル("Myfamily.xml" という名前)は、"family.xsd" と "children.xsd" という2つの異なるスキーマの構成要素を使用しています:
<?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 children.xsd">
<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
<childname>Cecilie</childname>
</children>
</person>
<person>
<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 children.xsd">
<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
<childname>Cecilie</childname>
</children>
</person>
<person>
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
上記の XML ファイルは有効です。これは、スキーマ "family.xsd" が "lastname" 要素の後のオプション要素によって "person" 要素を拡張することを許可しているためです。
<any> と <anyAttribute> はどちらも、拡張可能なドキュメントを作成するために使用できます!これらにより、ドキュメントはメインの XML スキーマで宣言されていない追加要素を含むことができます。
その他の拡張