[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Schema: defining occurences in choices?


choices in xs restriction
Hi Martin,

> I have a question on defining occurences in choices within schema.
[snip]
> where testentry should be a base type which gets then restricted.
> The name element must have certain fixed values. Depending on some
> of these values I want to define occurences/restrictions (minOccurs,
> maxOccurs, etc.) for the whole element. How can I do that?

I'm not sure which thing you want to affect the occurrence of -- is it
the <content> elements? If so, this is known as a "co-occurrence
constraint" -- the value of an element is affecting the content model
that you want to use. I'm afraid that co-occurrence constraints aren't
well supported in W3C XML Schema. The closest that you can do is:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

<xs:element name="test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="testentry" maxOccurs="unbounded">
        <xs:complexType>
          <xs:complexContent>
            <xs:restriction base="testType">
              <xs:element name="name">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:enumeration value="value1" />
                    <xs:enumeration value="value2" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="content" type="xs:string"
                          minOccurs="0" maxOccurs="unbounded" />
            </xs:restriction>
          </xs:complexContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:complexType name="testType">
  <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="content" type="xs:string"
                minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>

</xs:schema>

You can then have an additional layer of constraints expressed using
Schematron rules. For example:

<sch:rule context="testentry[name = 'value1']">
  <sch:assert test="content">
    A testentry whose name element child is 'value1' must have at
    least one content element child.
  </sch:assert>
</sch:rule>

Or, of course, you could swap to RELAX NG, which supports this kind of
constraint very easily:

element test {
  element testentry {
    (element name { "value1" }, element content { text }+) |
    (element name { "value2" }, element content { text }*)
  }+
}

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.