Element <anyAttribute> XSD

Element <anyAttribute> daje nam możliwość rozszerzenia dokumentu XML za pomocą atrybutów niezdefiniowanych w schema!

Element <anyAttribute>

Element <anyAttribute> daje nam możliwość rozszerzenia dokumentu XML za pomocą atrybutów niezdefiniowanych w schema!

Poniżej znajduje się przykładowy fragment z XML schema o nazwie "family.xsd". Pokazuje on deklarację elementu "person". Dzięki elementowi <anyAttribute> możemy dodać dowolną liczbę atrybutów do elementu "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>

Teraz, chcemy rozszerzyć element "person" poprzez atrybut "gender". W tym przypadku możemy to zrobić, nawet jeśli autor tego schematu nigdy nie deklarował żadnego atrybutu "gender".

Spójrz na ten plik schematu, nazwany "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>

Poniżej znajduje się ten XML (nazwany "Myfamily.xml"), który używa składników z różnych schematów, "family.xsd" i "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>

Powyższy plik XML jest poprawny, ponieważ schema "family.xsd" pozwala nam dodać atrybuty do elementu "person".

Elementy <any> i <anyAttribute> mogą być używane do tworzenia dokumentów rozszerzalnych! Pozwalają na zawarcie dodatkowych elementów, które nie zostały zadeklarowane w głównym XML schema.