1

I am looking for a solution within the XML 1.0 constraints to specify in XSD schema that a table should be filled completely or not at all. The table is optional and only needs to be filled when applicable, but if applicable all elements in the table are mandatory. I tried with specifying the table with minOccurrences = "0" and the elements within the table with minOccurrences = "1", but then I always have to provide a value for those elements.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Rogier
  • 11
  • 1

2 Answers2

1

The table is optional

So, minOccurs="0" on the table element.

and only needs to be filled when applicable

Some applicability constraints are not expressible in XSD 1.0.

If you can upgrade to XSD 1.1, you could use assertions to specify criteria for which the table cells may or may not be empty.

but if applicable all elements in the table are mandatory.

If the only applicability constraint is that the table cells must have content, you can accomplish the all-or-nothing aspect of your requirement with simple minOccurs="0" on the table and minOccurs="1" on cells. So, if the table is there, the cells must be there. You can also require that the cells be non-empty. All of this can be done in XSD 1.0.

More complex applicability constraints may require XSD 1.1 assertions.

See this answer for how to use xs:assert to specify conditions via every...satisfies that must be true over table cells.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

You can set constraints on attributes and elements as well.

Example of restriction on attributes -

<xsd:element name="remove">
<xsd:complexType>                        
    <xsd:attribute name="ref" use="optional"/>
    <xsd:attribute name="uri" use="optional"/>
    <xsd:assert test="(@ref and not(@uri)) or (not(@ref) and @uri)"/>            
</xsd:complexType>

Example of restriction on elements -

<xs:complexType name="IfBlockType">
<xs:sequence>
  <xs:element type="IfType" name="If"/>
  <xs:choice maxOccurs="unbounded" minOccurs="0">
    <xs:element type="ElseIfType" name="ElseIf"/>
  </xs:choice>
  <xs:choice maxOccurs="1" minOccurs="0">
    <xs:element type="ElseType" name="Else"/>
  </xs:choice>
</xs:sequence>