ホーム HTML CSS XML JAVASCRIPT   PHP SQL MORE...   リファレンス 事例集    

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" 要素を拡張しようと思います。 このケースでは、上記スキーマの作成者が任意の "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>

以下のXMLファイルは、("Myfamily.xml"と言います)2種類の異なるスキーマ ; "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>

"family.xsd" というスキーマが、"person" 要素に属性を追加することを可能に するので、上記 XML ファイルは妥当です。

<any> と <anyAttribute> 要素は、拡張可能なドキュメントを作るために使用します! これらは、文書がメインのXMLスキーマで宣言されていない追加の要素を含むことを可能にします。


« 前章へ 次章へ »