XML - XML Schema Wildcards and Open Content Models
1. Introduction
XML Schema is used to define the structure, elements, attributes and data types of an XML document. It helps ensure that an XML document follows a predefined set of rules. However, in real-world applications, XML documents may need to include additional elements or attributes that were not originally defined in the schema. This is especially common when different applications, systems or organizations exchange XML data.
XML Schema Wildcards and Open Content Models provide flexibility in such situations. They allow XML documents to contain elements or attributes that are not explicitly declared in the schema while maintaining the overall document structure. These features are useful when designing extensible XML documents that can adapt to changing requirements.
2. Understanding XML Schema Wildcards
XML Schema Wildcards are special declarations that allow an XML document to include elements or attributes that are not specifically defined in the schema. Instead of listing every possible element or attribute in advance, developers can use wildcards to accept additional content from specified namespaces.
The two main wildcard mechanisms in XML Schema are:
-
xs:any: Allows additional elements to appear within an XML structure. -
xs:anyAttribute: Allows additional attributes to be included in an XML element.
These declarations make XML schemas more flexible because developers do not need to modify the schema every time a new element or attribute is introduced.
For example, consider an XML document containing employee information. The schema may define employee ID, employee name and department as standard elements. However, another application may need to include an additional element called employeeLocation. By using xs:any, the schema can permit this additional element without requiring it to be explicitly declared in the original structure.
3. Understanding the xs:any Wildcard
The xs:any declaration is used inside an XML Schema complex type to allow additional child elements. It specifies where extra elements can appear and which namespaces are permitted.
Consider the following XML Schema example:
<xs:complexType name="EmployeeType">
<xs:sequence>
<xs:element name="employeeID" type="xs:int"/>
<xs:element name="employeeName" type="xs:string"/>
<xs:any minOccurs="0"
maxOccurs="unbounded"
processContents="lax"/>
</xs:sequence>
</xs:complexType>
In this example:
-
employeeIDdefines the employee identification number. -
employeeNamedefines the employee's name. -
xs:anyallows additional child elements after the employee name. -
minOccurs="0"means additional elements are optional. -
maxOccurs="unbounded"means any number of additional elements can be included. -
processContents="lax"instructs the XML validator to validate an additional element if its declaration is available; otherwise, it can accept the element without that declaration.
A corresponding XML document could look like this:
<employee>
<employeeID>101</employeeID>
<employeeName>John</employeeName>
<employeeLocation>Bangalore</employeeLocation>
<employeeExperience>5 Years</employeeExperience>
</employee>
Here, employeeLocation and employeeExperience are additional elements. The wildcard allows them to appear even though they are not explicitly declared in the complex type.
4. Understanding the xs:anyAttribute Wildcard
The xs:anyAttribute declaration allows an XML element to contain additional attributes that are not specifically defined in the schema.
It is generally used when different applications need to attach extra information to an XML element without changing its original definition.
Consider the following XML Schema example:
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="productName" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
A corresponding XML document could be:
<product category="Electronics" status="Available">
<productName>Laptop</productName>
<price>55000</price>
</product>
In this example, category and status are additional attributes. The xs:anyAttribute declaration allows these attributes even though they are not individually defined in the schema.
This approach is particularly useful in product catalogues, inventory systems and data exchange applications where additional information may be introduced over time.
5. Understanding Namespace Constraints in Wildcards
XML namespaces are used to distinguish elements and attributes that may have the same names but belong to different vocabularies.
Wildcards can specify which namespaces are allowed through the namespace attribute.
Some commonly used namespace options include:
##any: Allows elements or attributes from any namespace, including no namespace.
##other: Allows elements or attributes from namespaces other than the target namespace of the schema, generally excluding no-namespace content.
##targetNamespace: Allows content belonging to the target namespace of the schema.
##local: Allows elements or attributes that do not belong to any namespace.
For example:
<xs:any namespace="##other"
minOccurs="0"
maxOccurs="unbounded"
processContents="lax"/>
This declaration allows additional elements from namespaces other than the schema's target namespace.
Namespace restrictions are important when XML documents combine information from different applications or external systems. They help control which additional content can be included.
6. Understanding the processContents Attribute
The processContents attribute determines how an XML validator handles elements or attributes introduced through wildcards.
XML Schema provides three processing modes:
Strict Processing
In strict processing, the validator must find a declaration for the additional element or attribute and validate it according to the corresponding schema definition. If the declaration is unavailable, validation fails.
Example:
<xs:any processContents="strict"/>
Lax Processing
In lax processing, the validator checks the additional content when a suitable declaration is available. If no declaration is found, the content is accepted without further schema validation.
Example:
<xs:any processContents="lax"/>
Skip Processing
In skip processing, the validator does not attempt to validate the additional content against an XML Schema declaration.
Example:
<xs:any processContents="skip"/>
These processing modes allow developers to control the level of validation required for additional XML content.
7. Understanding XML Schema Open Content Models
Open Content Models are a feature introduced in XML Schema 1.1. They allow additional elements to appear within a complex type without requiring developers to insert an xs:any declaration manually at a particular position.
Unlike traditional wildcards, which are explicitly placed inside a sequence, choice or other content model, open content can allow additional elements alongside the elements already defined by the schema.
XML Schema 1.1 provides two main types of open content:
Interleave Open Content
Interleave mode allows additional elements to appear between the explicitly defined elements of a complex type.
For example, suppose an employee record contains employee ID, employee name and department. With interleave open content, an additional element such as employeeLocation can appear between these predefined elements without violating the content model.
Suffix Open Content
Suffix mode allows additional elements only after all explicitly defined elements have appeared.
For example, if the employee record contains employee ID, employee name and department, additional information such as employeeLocation or employeeExperience can appear after the department element.
An XML Schema 1.1 example is:
<xs:complexType name="EmployeeType">
<xs:openContent mode="interleave">
<xs:any processContents="lax"/>
</xs:openContent>
<xs:sequence>
<xs:element name="employeeID" type="xs:int"/>
<xs:element name="employeeName" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
</xs:sequence>
</xs:complexType>
In this example, the open content declaration permits additional elements to appear between the predefined employee elements.
8. Difference Between XML Schema Wildcards and Open Content Models
Although both features provide flexibility, they work in different ways.
|
Feature |
XML Schema Wildcards |
Open Content Models |
|---|---|---|
|
Introduction |
Available in XML Schema 1.0 |
Introduced in XML Schema 1.1 |
|
Declaration |
Uses |
Uses |
|
Element placement |
Depends on the position of the wildcard |
Can allow additional elements throughout the content model or at the end |
|
Flexibility |
Controlled by wildcard placement |
Provides broader structural flexibility |
|
Main purpose |
Allow specific undeclared elements or attributes |
Extend complex types with additional elements |
|
Usage |
Flexible XML structures |
Extensible XML document models |
9. Advantages of XML Schema Wildcards and Open Content Models
These features provide several advantages in XML-based applications.
Improved Extensibility: Developers can introduce new elements and attributes without redesigning the entire XML Schema.
Better System Integration: Different applications can exchange XML documents containing additional information while maintaining compatibility.
Reduced Maintenance: Minor changes in data requirements do not always require modifications to the original schema.
Flexible Data Exchange: Organizations can include application-specific information in shared XML documents.
Support for Evolving Applications: As software requirements change, XML structures can accommodate new information without completely changing their existing definitions.
10. Practical Applications of XML Schema Wildcards and Open Content Models
XML Schema Wildcards and Open Content Models are useful in several real-world applications.
Enterprise Data Integration: Large organizations exchange XML documents between departments and applications. Wildcards allow additional business information to be included without modifying the original schema for every change.
Web Services: XML-based web services may receive additional information from different clients. Wildcards can help accommodate extra elements and attributes in extensible message structures.
E-Commerce Applications: Product catalogues may contain standard information such as product name and price, along with additional details such as brand, colour, warranty and availability.
Healthcare Information Systems: Healthcare applications may exchange patient records containing standard medical information and additional fields required by different hospitals or healthcare providers.
Document Management Systems: XML-based document formats can use extensible structures to include additional metadata, author information and application-specific properties.
Conclusion
XML Schema Wildcards and Open Content Models are important features for developing flexible and extensible XML documents. Wildcards such as xs:any and xs:anyAttribute allow additional elements and attributes to be included in predefined XML structures. Open Content Models, introduced in XML Schema 1.1, provide greater flexibility by allowing additional elements to appear between or after predefined elements. Together, these features help developers build XML applications that can adapt to changing requirements, support different data sources and maintain compatibility between systems.