[xsd-users] Parsing issue

Boris Kolpackov boris at codesynthesis.com
Wed Nov 5 10:03:53 EST 2008


Hi Ildar,

Ildar Farkhshatov <ildar.farkhshatov at db.com> writes:

>  <xs:complexType name="Product">
>     <xs:sequence>
>       <xs:element name="Name" type="xs:string" />
>     </xs:sequence>
>  </xs:complexType>
>  <xs:complexType name="FxEuroOpt">
>     <xs:complexContent mixed="false">
>       <xs:extension base="Product">
>         <xs:sequence>
>           <xs:element name="Strike" type="xs:double" />
>           <xs:element name="ExpiryDate" type="xs:dateTime" />
>           <xs:element name="NotionalAmount" type="xs:double" />
>           <xs:element name="NotionalCurrency" type="Currency" />
>           <xs:element name="ZoneCut" type="Cut" />
>           <xs:element name="CurrencyPair" type="CurrencyPair" />
>           <xs:element name="SettleDate" type="xs:dateTime" />
>         </xs:sequence>
>       </xs:extension>
>     </xs:complexContent>
>   </xs:complexType>

>   <Product xsi:type="FxEuroOpt">
>     <Name>Euro</Name>
>     <Strike>1.0</Strike>
>     <NotionalAmount>1</NotionalAmount>
>     <ZoneCut>NY</ZoneCut>
>     <EffectiveDate>2008-11-29T00:00:00</EffectiveDate>
>     <ExpiryDate>2008-11-12T00:00:00</ExpiryDate>
>     <SettleDate>2008-11-14T00:00:00</SettleDate>
>     <CurrencyPair>
>       <Und>JPY</Und>
>       <Acc>USD</Acc>
>     </CurrencyPair>
>     <NotionalCurrency>USD</NotionalCurrency>
>     <PayCurrency>USD</PayCurrency>
>   </Product>


Ok, now it all makes sense. The XML fragment you showed earlier
is not valid per this schema (there are actually two problems:
first, it contains the EffectiveDate and PayCurrency elements 
that are not allowed by the schema and second, the elements are 
not in the order that is prescribed by the schema).

Unfortunately, there is no way to successfully parse this fragment
with the generated code even when the full XML Schema validation
is disabled. In the parsing code we have to bail out when we see 
an unknown element. That's the only reasonably efficient way to 
parse the base/derived content (the base type should stop parsing 
on the first unknown element so that the derived type can continue).

If you have control over the production of this XML, one way to 
work around the problem is to move all unknown elements to the end.

If you can change the schema (even if only for the purpose of code
generation) then you can add the unknown elements as optional:

 <xs:complexType name="FxEuroOpt">
    <xs:complexContent mixed="false">
      <xs:extension base="Product">
        <xs:sequence>
          <xs:element name="Strike" type="xs:double" />
          <xs:element name="ExpiryDate" type="xs:dateTime" />
          <xs:element name="NotionalAmount" type="xs:double" />
          <xs:element name="NotionalCurrency" type="Currency" />
          <xs:element name="ZoneCut" type="Cut" />
          <xs:element name="CurrencyPair" type="CurrencyPair" />
          <xs:element name="SettleDate" type="xs:dateTime" />

          <xs:element name="EffectiveDate" type="xs:dateTime" minOccurs="0"/>
          <xs:element name="PayCurrency" type="Currency" minOccurs="0"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

Boris




More information about the xsd-users mailing list