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>
次に、「person」要素を「children」要素で拡張します。この場合、上記のスキーマの作成者が「子」要素を宣言していなくても、そうすることができます。
「children.xsd」と呼ばれるこのスキーマファイルを見てください。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://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」と呼ばれます)は、2つの異なるスキーマのコンポーネントを使用しています。「family.xsd」および「children.xsd」:
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.microsoft.com family.xsd
https://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」で「person」要素を「lastname」要素の後にオプションの要素で拡張できるため、有効です。
<any>要素と<anyAttribute>要素は、EXTENSIBLEドキュメントを作成するために使用されます。これらを使用すると、メインのXMLスキーマで宣言されていない追加の要素をドキュメントに含めることができます。