Inheriting an attribute from an "optional" contain

This should cover W3C XML Schema, Relax NG and DTD related problems.
Posts: 107
Location: College Park, MD, United States

Inheriting an attribute from an "optional" contain

Wed Oct 24, 2007 9:44 pm

I'm writing a schema for a school project, and it's intended to help automate what is a very tedious process of writing an Individual Studies Proposal, which involves looking up information between many different sources.

Here is my desired setup:
  • I want to model a Course.
  • The base definition should be just the information for a Course: it has a Department, a Number, a number of Credits, a Title, and a Description.
  • Optionally, I want the ability to group several Course elements under a Courses element. This container should be able to specify the Department attribute, which should mean that all contained Course elements inherit that value (and are not allowed to redeclare the Department value in instance documents)
  • I want to allow the Courses element to optionally contain other Courses elements, so if the author of an instance document wants a root Courses element, and then several Courses elements within that--each with their own Department attribute specified--this could be done

Code: Select all

<xs:element name="course" type="courseType" />
   <xs:element name="courses" type="coursesType" />
      Now, define all TYPES.
      (If unsure whether to write a type or an element, go with the type. 
      You can always make an element based off of that type definition.)
   <xs:complexType name="coursesType">
         <xs:element ref="course" minOccurs="1" maxOccurs="unbounded">
      <xs:attribute name="dept" type="xs:IDREF" use="required" />
   <xs:complexType name="courseType">
      <xs:attributeGroup ref="course-attributes" />
      <xs:key name="deptKey">
         <xs:selector xpath="dept"></xs:selector>
         <xs:field xpath="@id"></xs:field>
      <xs:keyref name="dept" refer="deptKey">
         <xs:selector xpath="dept"></xs:selector>
         <xs:field xpath="@dept"></xs:field>

This is where I got stuck in my schema. I was adding in the Courses definition when I couldn't figure out how to redefine restrictions, or give the ability to contained Course elements to inherit their Department value from their parent Courses element.

Could somebody show me how this is done?
-- Zearin

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 1 guest