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 파일은 유효합니다. 이는 "family.xsd" 스키마가 "person" 요소에 속성을 추가할 수 있음을 허용하기 때문입니다.

<any>과 <anyAttribute>은 확장 가능한 문서를 만들기 위해 사용될 수 있습니다! 이들은 문서가 주 XML Schema에서 선언되지 않은 추가 요소를 포함할 수 있게 합니다.