How to add an attribute to an xsd:any element
Question
How do you add an attribute to an xsd:any element? For example, given the following:
<xsd:element name="requests">
<xsd:complexType>
<xsd:sequence>
<xsd:any namespace="http://xxx.yyy.com" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
How do you add as an attribute to any so that the following xml can be validated against the schema without errors:
<requests>
<operation count="1">
<requests>
Solution
xsd:any cannot include any attribute declarations, because it essentially allows any element that is defined within the namespace "http://xxx.yyy.com" to be declared within the sequence. If you are not using a separate XSD to validate that namespace, then you can simply use the following in place of xsd:any:
<xsd:element name="operation">
<xsd:complexType>
<xsd:attribute name="count" type="nonNegativeInteger" use="required"/>
</xsd:complexType>
</xsd:element>
Otherwise, you will want to declare a namespace prefix for "http://xxx.yyy.com" at the top of your XSD and refer to the element within that schema instead of xsd:any. So, if the schema for "http://xxx.yyy.com" includes the following declaration:
<xsd:complexType name="operationType">
<xsd:attribute name="count" type="nonNegativeInteger" use="required"/>
</xsd:complexType>
Then you could reference this type in your XSD:
<xsd:element name="requests">
<xsd:complexType>
<xsd:sequence>
<xsd:element type="optype:operationType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
OTHER TIPS
If you're saying you want to allow any element as a child so long as it has a count attribute, then you can't do that in XSD 1.0. You can do it in XSD 1.1 (currently supported in Saxon and Xerces) with an assertion:
<xs:assert test="every $x in * satisfies (exists($x/@count) and $x/@count castable to xs:integer)"/>