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>
次に、「person」要素を「eyecolor」属性で拡張します。この場合、上記のスキーマの作成者が「eyecolor」属性を宣言していなくても、そうすることができます。
「attribute.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:attribute name="eyecolor">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="blue|brown|green|grey"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>
以下のXMLファイル(「Myfamily.xml」と呼ばれます)は、2つの異なるスキーマのコンポーネントを使用しています。「family.xsd」および「attribute.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 attribute.xsd">
<person eyecolor="green">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person eyecolor="blue">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
上記のXMLファイルは、スキーマ「family.xsd」で「person」要素に属性を追加できるため、有効です。
<any>要素と<anyAttribute>要素は、EXTENSIBLEドキュメントを作成するために使用されます。これらを使用すると、メインのXMLスキーマで宣言されていない追加の要素をドキュメントに含めることができます。