How can I specify an element for an attribute that indicates how many children it contains in an XML schema?

Is it possible?

  • I know that it is possible to make a restriction based on a regular expression, but it's not him
  • I know that it can be declared as a foreign key calculated by XPath, but it seems to be unique

Exemple:

<root children="2"> <child /> <child /> </root> 
+7
source share
2 answers

W3C Schema 1.0 does not have the ability to limit attribute values โ€‹โ€‹based on an instance document.

Schematron is a great tool for verifying that documents adhere to such custom validation scripts.

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> 
+5
source

XSD 1.1 allows you to express this type 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.

+6
source

All Articles