XSD Constraints

Discussion in 'XML' started by Steve Fletcher, Feb 1, 2008.

  1. 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:attribute name="Operation" type="ns1:OpType" use="required"/>
    <xs:attribute name="Sequence" type="ns1:SequenceType" use="optional"/
    >

    </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>
    Steve Fletcher, Feb 1, 2008
    #1
    1. Advertising

  2. Steve Fletcher

    Pavel Lepin Guest

    Steve Fletcher <> wrote in
    <>:
    > 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?

    --
    <>There is no phenotype</>
    Pavel Lepin, Feb 1, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,072
    Markus
    Nov 22, 2005
  2. Roland
    Replies:
    1
    Views:
    1,527
    Oleg Tkachenko
    Sep 25, 2003
  3. Rick Razzano

    XSD document for XSD defintion

    Rick Razzano, Sep 26, 2003, in forum: XML
    Replies:
    1
    Views:
    457
    C. M. Sperberg-McQueen
    Sep 26, 2003
  4. Replies:
    1
    Views:
    837
    Martin Honnen
    Jan 14, 2004
  5. Peter Aberline

    xsd:any as a child of xsd:all

    Peter Aberline, Apr 5, 2004, in forum: XML
    Replies:
    0
    Views:
    763
    Peter Aberline
    Apr 5, 2004
Loading...

Share This Page