XSD - <anyAttribute>

L'élément <xs: anyAttribute> est utilisé pour étendre la fonctionnalité XSD. Il est utilisé pour étendre un élément complexType défini dans un xsd par un attribut qui n'est pas défini dans le schéma.

Prenons un exemple - person.xsd a défini personÉlément complexType. attributes.xsd a définiage attribut.

person.xsd

<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
   targetNamespace = "http://www.tutorialspoint.com"
   xmlns = "http://www.tutorialspoint.com"
   elementFormDefault = "qualified">

   <xs:element name = "person">
      <xs:complexType >
         <xs:sequence>
            <xs:element name = "firstname" type = "xs:string"/>
            <xs:element name = "lastname" type = "xs:string"/>
            <xs:element name = "nickname" type = "xs:string"/>     
         </xs:sequence>
      </xs:complexType>
   </xs:element>
	
</xs:schema>

attributes.xsd

<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
   targetNamespace = "http://www.tutorialspoint.com"
   xmlns = "http://www.tutorialspoint.com"
   elementFormDefault = "qualified">

   <xs:attribute name = "age">
      <xs:simpleType>
         <xs:restriction base = "xs:integer">
            <xs:pattern value = "[0-100]"/>
         </xs:restriction>
      </xs:simpleType>
   </xs:attribute>
	
</xs:schema>

Si nous voulons définir une personne avec l'âge en XML, la déclaration suivante sera invalide.

person.xml

<?xml version = "1.0"?>
<class xmlns = "http://www.tutorialspoint.com"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.tutorialspoint.com person.xsd
   http://www.tutorialspoint.com attributes.xsd">  

   <person age = "20">
      <firstname>Dinkar</firstname>
      <lastname>Kad</lastname>
      <nickname>Dinkar</nickname>  
   </person>
	
</class>

Utilisez <xs: anyAttribute>

Afin de valider ci-dessus person.xml, ajoutez <xs: anyAttribute> à person élément dans person.xsd.

person.xsd

<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
   targetNamespace = "http://www.tutorialspoint.com"
   xmlns = "http://www.tutorialspoint.com"
   elementFormDefault = "qualified">

   <xs:element name = "person">
      <xs:complexType >
         <xs:sequence>
            <xs:element name = "firstname" type = "xs:string"/>
            <xs:element name = "lastname" type = "xs:string"/>
            <xs:element name = "nickname" type = "xs:string"/>            
         </xs:sequence>
         <xs:anyAttribute/>
      </xs:complexType>
   </xs:element>

</xs:schema>

Maintenant person.xml sera validé contre person.xsd et attributes.xsd.