You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by "Kirkbride, David BGI SF" <Da...@barclaysglobal.com> on 2004/08/26 19:49:49 UTC

Interesting substitution group behavior

Radu,

Thanks for the SubstitutionGroup FAQ, it is quite helpful.  I have a small addition for it. I've been attempting to create an example instance document based on a Schema that uses substitution groups quite extensively.  Here's what I've discovered works (and what doesn't)

'Swap' is an XMLBeans generated class that is derived from another XMLBeans generated class: 'Product'.  Product is abstract. If I create a new Swap, set it into its container (Trade) as a Product, call setName(...) as you suggest, then proceed to set swap's attributes and elements as normal, things appear to work, at first. Notice that the last line of the first section (where I retrieve the swap reference) is commented out; more on this later:

   // create a Swap, alter it, then get a reference to it
   Swap swap = Swap.Factory.newInstance();
   trade.setProduct(swap);
   trade.getProduct().newCursor().setName(
      new QName("http://www.fpml.org/2004/FpML-4-1", "swap", "fpml"));
//   swap = (Swap)trade.getProduct();

   // add information to the swap.
   ProductType productType = swap.addNewProductType();
   swap.setId("aSwapId");

When I'm all finished adding elements, swap.validate(), returns true, and a printout of swap.xmlText() shows that all expected elements are present:

<xml-fragment id="aSwapId">
   <fpml:productType xmlns:fpml="http://www.fpml.org/2004/FpML-4-1"/>
      <fpml:swapStream xmlns:fpml="http://www.fpml.org/2004/FpML-4-1">
      <fpml:payerPartyReference href="CHASE"/>
      <fpml:receiverPartyReference href="BGI"/>
      <fpml:calculationPeriodDates id="floatingCalcPeriodDates">
      ... 
   </fpml:swapStream>
</xml-fragment>

However, (and here's where the problem crops up) when I attempt to validate swap's container object, trade.validate() returns false.  A printout of trade.xmlText() shows:

<xml-fragment id="aTradeId" xmlns:fpml="http://www.fpml.org/2004/FpML-4-1">
  <fpml:tradeHeader>
     ...
  </fpml:tradeHeader>
  <fpml:swap xsi:type="fpml:Swap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</xml-fragment>

Notice that trade isn't able to access any of the information within the swap.  I supposed that this was because trade.setProduct(swap) is actually creating a copy, rather than just setting a reference to the existing object.  So, If I uncomment the line in the first section and retrieve a swap reference back from trade, I receive the following error when attempting to validate the new swap reference:

Element 'product@http://www.fpml.org/2004/FpML-4-1' is abstract and cannot be used in an instance.

After much gnashing of teeth though, I discovered that I could do things in this order, and that they work!

   Swap swap = Swap.Factory.newInstance();
   ProductType productType = swap.addNewProductType();
   swap.setId("aSwapId");
   // ... set the rest of swap's required elements.

   // now finally, add the swap to its container.
   trade.setProduct(swap);
   trade.getProduct().newCursor().setName(
      new QName("http://www.fpml.org/2004/FpML-4-1", "swap", "fpml"));

Performing the actions in this order does the trick; I'm able to get both a valid swap and a valid trade, and a trade.getXml() looks like:

<xml-fragment id="aTradeId" xmlns:fpml="http://www.fpml.org/2004/FpML-4-1">
  <fpml:tradeHeader>
     ...
  </fpml:tradeHeader>
  <fpml:swap xsi:type="fpml:Swap" id="aSwapId" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <fpml:productType/>
    <fpml:swapStream xmlns:fpml="http://www.fpml.org/2004/FpML-4-1">
       ...
    </fpml:swapStream>
  </fpml:swap>
</xml-fragment>

-----

Thanks again for the great FAQ, hopefully someone finds this information helpful (and hopefully all of this becomes irrelevant after the release of XMLBeans 2.0 ;o)

-dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org