Is it even possible?
Exemple:
<root children="2">
<child />
<child />
</root>
Attributes are by default optional. But to make an attribute mandatory, "use" attribute can be used.
Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.
A complex type is a container for other element definitions, this allows you to specify which child elements an element can contain. This allows you to provide some structure within your XML documents.
XSD 1.1 allows you to express this kind of constraint:
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="child" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="children" type="xs:integer"/>
</xs:complexType>
<xs:assert test="@children = count(child)"/>
</xs:element>
XSD 1.1 is currently implemented in Saxon and Xerces.
W3C Schema 1.0 doesn't have the ability to constrain the attribute values based upon the instance document.
Schematron is a great tool for validating that documents adhere to such custom validation scenarios.
For example:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern>
<rule context="root[@children]">
<assert
id="children-value"
test="@children=count(child)"
flag="error">
The root/@children value must be equal to the number of child elements.
</assert>
</rule>
</pattern>
</schema>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With