Pergunta

Is it possible to define value restrictions between two elements in XML Schema?

Like in the following XSD:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://mydomain.com"
  xmlns="http://mydomain.com"
  elementFormDefault="qualified">

  <xs:element name="form">
    <xs:complexType>
      <xs:all>
        <xs:element name="metric1" type="xs:integer" />
        <xs:element name="metric2" type="xs:integer" />
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

Is there a way to define that the value of metric2 should greater than the value of metric 1? So that the following XML document would fail on validation:

<?xml version="1.0"?>
<form xmlns="http://www.ziya.gov.cn">
  <metric1>5</metric1>
  <metric2>3</metric2>
</form>
Foi útil?

Solução

In XSD 1.1, define an assertion

<xs:assert test="metric2 gt metric1"/>

as part of the complexType definition.

XSD 1.1 is currently supported in Xerces, Saxon, and Altova.

If you're stuck with an XSD 1.0 processor then this constraint can't be defined.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top