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」要素を拡張したいと考えています。この場合、schemaの作者が「gender」属性を宣言していない場合でも、このようにすることができます。

このschemaファイル「attribute.xsd」を見てください:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.codew3c.com"
xmlns="http://www.codew3c.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」と呼ばれる)は、異なるschemaの要素を使用しています。"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.codew3c.com attribute.xsd">
<person gender="female">
<firstname>Jane</firstname>
<lastname>Smith</lastname>
</person>
<person gender="male">
<firstname>David</firstname>
<lastname>Smith</lastname>
</person>
</persons>

この XML ファイルは有効です。これは、schema "family.xsd" が "person" 要素に属性を追加することを許可しているためです。

<any> と <anyAttribute> は、拡張可能なドキュメントを作成するために使用できます!これにより、主 XML schema で宣言されていない追加要素をドキュメントに含めることができます。