XSD <anyAttribute> Element
- Previous Page XSD <any>
- Next Page XSD Element Substitution
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema!
<anyAttribute> element
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema!
The following example is a fragment from an XML schema named "family.xsd". It shows a declaration for the "person" element. We can add an arbitrary number of attributes to the "person" element by using the <anyAttribute> element:
<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>
Now, we hope to extend the "person" element through the "gender" attribute. In this case, we can do this even if the author of this schema never declared any "gender" attribute.
Please see this schema file, named "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>
Below is this XML (named "Myfamily.xml"), which uses components from different schemas, "family.xsd" and "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>
The above XML file is valid because the schema "family.xsd" allows us to add attributes to the "person" element.
Both <any> and <anyAttribute> can be used to create extensible documents! They enable documents to include additional elements that are not declared in the main XML schema.
- Previous Page XSD <any>
- Next Page XSD Element Substitution