XSD <anyAttribute>
Allowing flexible, undefined attributes
⚡ What is <anyAttribute>?
The <anyAttribute> element allows any attribute from specified namespaces on an element. It provides flexibility for adding custom attributes without modifying the schema, perfect for extensible systems.
<!-- Any attribute allowed -->
<xs:anyAttribute namespace="##any" />
Basic <anyAttribute> Usage
The <anyAttribute> element lets you add any attribute to an element, making your schema flexible and extensible for future needs or custom implementations.
🔸 Simple <anyAttribute> Example
<xs:element name="widget">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
<xs:anyAttribute />
</xs:complexType>
</xs:element>
Valid XML:
<widget id="W001" color="blue" size="large" custom="value">
<name>Super Widget</name>
</widget>
🔹 Namespace Control
Control which namespaces attributes can come from using the namespace attribute. This restricts the source of custom attributes:
<xs:element name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
<!-- Allow attributes from any namespace -->
<xs:anyAttribute namespace="##any" />
<!-- OR allow from specific namespace -->
<!-- <xs:anyAttribute namespace="http://example.com/custom" /> -->
<!-- OR allow from other namespaces only -->
<!-- <xs:anyAttribute namespace="##other" /> -->
</xs:complexType>
</xs:element>
Valid XML:
<product id="P123" featured="true" discount="10" category="electronics">
<title>Laptop</title>
<price>999.99</price>
</product>
🔹 Process Content Control
The processContents attribute controls validation strictness for custom attributes:
<xs:element name="component">
<xs:complexType>
<xs:sequence>
<xs:element name="type" type="xs:string" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<!-- Strict: Attributes must be defined in schema -->
<xs:anyAttribute namespace="##any" processContents="strict" />
<!-- Lax: Validate if schema exists, otherwise allow -->
<!-- <xs:anyAttribute namespace="##any" processContents="lax" /> -->
<!-- Skip: No validation, allow anything -->
<!-- <xs:anyAttribute namespace="##any" processContents="skip" /> -->
</xs:complexType>
</xs:element>
🔹 Combining with Regular Attributes
You can define specific required attributes and allow additional custom attributes for flexibility:
<xs:element name="button">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<!-- Required attributes -->
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="submit" />
<xs:enumeration value="button" />
<xs:enumeration value="reset" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<!-- Optional attributes -->
<xs:attribute name="disabled" type="xs:boolean" />
<!-- Allow any additional attributes -->
<xs:anyAttribute processContents="skip" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Valid XML:
<button id="btn1" type="submit" disabled="false"
class="primary" data-action="save" aria-label="Submit Form">
Submit
</button>
🔹 Complete Example: Configurable Element
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="setting" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<!-- Core attributes -->
<xs:attribute name="key" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:string" />
<!-- Allow custom attributes for metadata -->
<xs:anyAttribute namespace="##any" processContents="lax" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="version" type="xs:string" use="required" />
<xs:anyAttribute processContents="skip" />
</xs:complexType>
</xs:element>
</xs:schema>
Valid XML:
<config version="1.0" environment="production" author="admin">
<setting key="timeout" type="integer" unit="seconds" priority="high">30</setting>
<setting key="debug" type="boolean" category="development">false</setting>
<setting key="apiUrl" type="string" protocol="https">api.example.com</setting>
</config>
🔹 Common Use Cases
Styling Attributes
CSS classes, styles, themes
Data Attributes
Custom data-* attributes
Accessibility
ARIA attributes
Configuration
Custom settings, metadata