XSD Constraints

S

Steve Fletcher

Hello,

I'm writing a schema, and I want to apply some contraints to the
combination of values that can be supplied for the attributes for one
of the elements. The problem concerns the Operation element in the xml
fragment below. The element has two attrbiutes, and I want to apply
some rules that ensure that the values provided are in agreement with
one another. For example, I want ensure the CREATE, UPDATE and DELETE
values for the Operation attribute are provided with a Sequence
number, but that the CREATEUPDATE value of Operation attribute is not
have a Sequence Number provided.

I want to <b>allow</b> only the following combinations:
<ns1:Operation Operation='CREATE' Sequence=(any +ve int)/>
<ns1:Data>Data</ns1:Data>
....
<ns1:Operation Operation="UDPATE' Sequence=(any +ve int)/>
<ns1:Data>Data</ns1:Data>
....
<ns1:Operation Oprtation='DELETE' Sequence=(any +ve int)/>
<ns1:Data>Data</ns1:Data>
....
<ns1:Operation Operation='CREATEUPDATE'/>
<ns1:Data>Data</ns1:Data>

but <b>prevent<b/> the following xml
<ns1:Operation Operation='CREATEUPDATE' Sequence=(any +ve int)/>
<ns1:Data>Data</ns1:Data>


Does anyone know how this is achieved?


--Fragment--
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.world.com" xmlns:xs="http://www.w3.org/
2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:ns1="http://world.com/schema.xsd" targetNamespace="http://
world.com/schema.xsd" elementFormDefault="qualified" version="1.5">
<xs:element name="ScriptMark" type="ns1:MarkType"/>
<xs:complexType name="MarkType">
<xs:sequence>
<xs:element name="Operation" type="ns1:OperationType"/>
<xs:element name="Data" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="OperationType">
</xs:complexType>
<xs:simpleType name="OpType">
<xs:restriction base="xs:string">
<xs:enumeration value="CREATEUPDATE"/>
<xs:enumeration value="CREATE"/>
<xs:enumeration value="UPDATE"/>
<xs:enumeration value="DELETE"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="SequenceType">
<xs:restriction base="xs:int">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
 
P

Pavel Lepin

Steve Fletcher said:
I'm writing a schema, and I want to apply some contraints
to the combination of values that can be supplied for the
attributes for one of the elements. The problem concerns
the Operation element in the xml fragment below. The
element has two attrbiutes, and I want to apply some rules
that ensure that the values provided are in agreement with
one another. For example, I want ensure the CREATE, UPDATE
and DELETE values for the Operation attribute are provided
with a Sequence number, but that the CREATEUPDATE value of
Operation attribute is not have a Sequence Number
provided.

I want to <b>allow</b> only the following combinations:

Note that HTML markup is inappropriate in a Content-Type:
text/plain; charset=ISO-8859-1 document.
<ns1:Operation Operation='CREATE' Sequence=(any +ve int)/>
<ns1:Data>Data</ns1:Data>
...
<ns1:Operation Operation="UDPATE' Sequence=(any +ve int)/>
<ns1:Data>Data</ns1:Data>
...
<ns1:Operation Oprtation='DELETE' Sequence=(any +ve int)/>
<ns1:Data>Data</ns1:Data>
...
<ns1:Operation Operation='CREATEUPDATE'/>
<ns1:Data>Data</ns1:Data>

but <b>prevent<b/> the following xml
<ns1:Operation Operation='CREATEUPDATE' Sequence=(any +ve
int)/> <ns1:Data>Data</ns1:Data>

Does anyone know how this is achieved?

There's a number of traditional ways of dealing with
requirements like this.

1). Use <create>, <update>, <delete> and <createupdate>
elements instead of a generic <Operation> element.
2). Check on the application side.
3). Use a more powerful schema definition language, such
as RELAX NG or Schematron constraint checking
language.

By the way, just wondering, - are your <Data> elements
supposed to provide additional information about the
preceding <Operation> element?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top