开发者

Defining element references in an XSD

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-con开发者_高级运维tract-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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜