tags:

views:

11

answers:

1

How would one write an xsd to represent the xml shown below (taken from http://static.springsource.org/spring-ws/sites/2.0/reference/html/why-contract-first.html)?

<flight number="KL1117">
  <passengers>
    <passenger>
      <name>Arjen Poutsma</name>
      <flight href="KL1117" />
    </passenger>
  </passengers>
</flight>

In this case, passenger.flight is referencing another flight element (in this case, the grandparent of passenger). How do you actually handle this when writing xsds? This could cover cases of cyclical references or just for convenience to keep repetition of elements that represent complex types to a minimum by using references.

+1  A: 

There is no problem with this unless you use a modelling approach where you reference global elements (aka "salami slice" schema), since you cannot define two global elements with the same name. If you use a slightly more type-based approach, this is no problem (note, I'm leaving out the repeating passenger list for clarity):

<xs:complexType name="FlightDefinition">
    <xs:sequence>
        <xs:element name="passenger" type="Passenger"/>
    </xs:sequence>
    <xs:attribute name="number" type="xs:ID" use="required"/>
</xs:complexType>
<xs:complexType name="Passenger">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="flight" type="FlightReference"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="FlightReference">
    <xs:attribute name="href" type="xs:IDREF" use="required"/>
</xs:complexType>
<xs:element name="flight" type="FlightDefinition"/>

By the way: I take it the flight reference inside the passenger will point somewhere else, it's redundant if the passenger is contained within a flight ancestor.

xcut
That seems to work - my problem is that I was "salami slicing" and using element refs everywhere in my xsd.
whaley