Value arithmetic required - elements

From W3C Wiki
Jump to: navigation, search

Value arithmetic required - elements (a co-constraint use case)

Require the values of specified children to satisfy an equation or an inequality.

Cf. Value arithmetic required - attributes, which formulates a similar use case involving attributes (and an inequality).


Other use cases: Co-constraint Use Cases

source: Margrethe Fossberg [1]

Description

   I would like, say, elements a, b, and c to have a min
   value of 0 and max value of 15, but the total sum of a, b and c cannot
   exceed 30.  Is this possible at all?

Analysis

(Add your analysis here; see your name in pixels!)

Possible solutions

Relax NG

Schematron

Check clause

If the elements a, b, c are siblings, then a simple check clause on the parent will do:

 <xsd:simpleType name="max15">
  <xsd:restriction base="xsd:decimal">
   <xsd:minInclusive value="0.0"/>
   <xsd:maxInclusive value="15.0">
   </xsd:maxInclusive>
  </xsd:restriction>
 </xsd:simpleType>
 <xsd:element name="container">
  <xsd:complexType>
   <xsd:annotation><xsd:appinfo><sch:pattern><sch:rule>
       <sch:assert test="(./a + ./b + ./c) <= 30">
	The sum of a, b, and c must not exceed 30.
       </sch:assert>
      </sch:rule></sch:pattern></xsd:appinfo></xsd:annotation>
   <xsd:sequence>
    <xsd:element name="a" type="this:max15"/>
    <xsd:element name="b" type="this:max15"/>
    <xsd:element name="c" type="this:max15"/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>

SchemaPath

If T is the name of the type assigned under normal circumstances to the container element, then we can write

<xsd:element name="container">
  <xsd:alt cond="./a + ./b + ./c > 30.00" type="xsd:error"/>
  <xsd:alt type="T"/>
</xsd:element>

Conditional Type