Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » XML substitution groups and FeatureMaps(Using generated API with featureMaps for substitution groups)
XML substitution groups and FeatureMaps [message #645991] Tue, 21 December 2010 13:52 Go to next message
TobyS is currently offline TobySFriend
Messages: 4
Registered: July 2009
Junior Member
I'm new to EMF and no XML expert either, but am trying to use EMF to generate some FpML XML. I think I've grasped the basics, but I'm struggling with adding elements which are part of a substitution group in the XSD specification (abstract in ECore modelling terms).

So, I've generated the EMF model based on the FpML XSD schema and that went fine, and I've then generated the model code from that. I've then tried to programatically build an instance of the model, and then write that out as an XML file. However, I'm unable to see how to add the substitution group in.

I've boiled the problem down to the basics, so in the example below it's not full FpML but a massively pared down part of it.

In this specific example I'm building the "Trade" part of the model, which contains an abstract Product item, in my case the concrete implementation of which is a CreditDefaultSwap.

This is what I'm aiming to produce:
<?xml version="1.0" encoding="UTF-8"?>

<FpML 
    xmlns="http://www.fpml.org/2010/FpML-4-8" 
    xmlns:fpml="http://www.fpml.org/2010/FpML-4-8" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:type="DataDocument"
    xsi:schemaLocation="http://www.fpml.org/2010/FpML-4-8 ../xsd/test.xsd http://www.w3.org/2000/09/xmldsig# ../xsd/xmldsig-core-schema.xsd">

<trade>
  <creditDefaultSwap>
    <cdsName>Toby CDS</cdsName>
  </creditDefaultSwap>
</trade>

</FpML>


Here's what I hope are the pertinent parts of my code so far:
        FpmlFactory factory = FpmlFactory.eINSTANCE;    
        Trade trade = factory.createTrade();          
        CreditDefaultSwap cds = factory.createCreditDefaultSwap();
        cds.setCdsName("Toby CDS");
        FeatureMap featureMap = trade.getProductGroup();
        featureMap.add( TobyPackage.Literals.TRADE__PRODUCT, cds);
        / /  -> java.lang.RuntimeException: Invalid entry feature 'Trade.product'

        // code would then go on to save the resource which is fine


My understanding is that for substition groups, I need to use a FeatureMap. I'm not sure I've understood quite how these work though, as the above code is throwing an exception as a per the comment in the code. I've looked at the EMF book and saw a bit about mixed featureMaps which might be relevant but I couldn't quite get my head around it:

Quote:

If the reference to a head or abstract element is nested within a schema component for which a feature map would already be produced (e.g., if the containing complex type is mixed), the resulting feature map EAttribute then derives from that other feature map.


I've massively simplified the XSD schema to extract the problem parts, the two XSD files are at the end of this post.

Can someone advise where I'm going wrong please? I've tried this on the full FpML model too with the same result (Invalid entry feature 'Trade.product')

test.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://www.fpml.org/2010/FpML-4-8"    
    xmlns:fpml="http://www.fpml.org/2010/FpML-4-8"  
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    ecore:nsPrefix="fpml" ecore:package="org.fpml" ecore:documentRoot="FpML" 
    targetNamespace="http://www.fpml.org/2010/FpML-4-8" attributeFormDefault="unqualified" elementFormDefault="qualified">
 
  <xsd:include schemaLocation="test-shared.xsd" />

  <xsd:element name="FpML" type="Document"/>

</xsd:schema>



and
test-shared.xsd
<?xml version="1.0" encoding="utf-8"?>

<xsd:schema xmlns="http://www.fpml.org/2010/FpML-4-8" 
  targetNamespace="http://www.fpml.org/2010/FpML-4-8" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" 
  ecore:nsPrefix="fpml" ecore:package="org.fpml" ecore:documentRoot="FpML"  
  elementFormDefault="qualified" attributeFormDefault="unqualified"> 

  <xsd:complexType name="Document" abstract="true"/>    
  
  <xsd:complexType name="DataDocument">    
    <xsd:complexContent>
      <xsd:extension base="Document">
        <xsd:sequence>       
          <xsd:choice>
            <xsd:sequence>
              <xsd:element name="trade" type="Trade" minOccurs="0" maxOccurs="unbounded">             
              </xsd:element>              
            </xsd:sequence>           
          </xsd:choice>         
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  
   <xsd:complexType name="Trade">   
    <xsd:sequence>     
      <xsd:element ref="product" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="product" type="Product" abstract="true"/>
   
  <xsd:complexType name="Product" abstract="true"/>     
        
  <xsd:element name="creditDefaultSwap" type="CreditDefaultSwap" substitutionGroup="product"/>   
      
  <xsd:complexType name="CreditDefaultSwap">
    <xsd:complexContent>
      <xsd:extension base="Product">
        <xsd:sequence>
            <xsd:element name="cdsName" type="xsd:string"/>
        </xsd:sequence>
      </xsd:extension>      
    </xsd:complexContent>
  </xsd:complexType>
  
</xsd:schema>
  


Many thanks in advance to anyone who can help, I've really hit a brick wall with this.
Re: XML substitution groups and FeatureMaps [message #646044 is a reply to message #645991] Tue, 21 December 2010 17:15 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Toby,

Comments below.


TobyS wrote:
> I'm new to EMF and no XML expert either, but am trying to use EMF to
> generate some FpML XML. I think I've grasped the basics, but I'm
> struggling with adding elements which are part of a substitution group
> in the XSD specification (abstract in ECore modelling terms).
Aren't those fun?!
>
> So, I've generated the EMF model based on the FpML XSD schema and that
> went fine, and I've then generated the model code from that. I've then
> tried to programatically build an instance of the model, and then
> write that out as an XML file. However, I'm unable to see how to add
> the substitution group in.
>
> I've boiled the problem down to the basics, so in the example below
> it's not full FpML but a massively pared down part of it.
>
> In this specific example I'm building the "Trade" part of the model,
> which contains an abstract Product item, in my case the concrete
> implementation of which is a CreditDefaultSwap.
>
> This is what I'm aiming to produce:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <FpML xmlns="http://www.fpml.org/2010/FpML-4-8"
> xmlns:fpml="http://www.fpml.org/2010/FpML-4-8"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:type="DataDocument"
> xsi:schemaLocation="http://www.fpml.org/2010/FpML-4-8
> ../xsd/test.xsd http://www.w3.org/2000/09/xmldsig#
> ../xsd/xmldsig-core-schema.xsd">
>
> <trade>
> <creditDefaultSwap>
> <cdsName>Toby CDS</cdsName>
> </creditDefaultSwap>
> </trade>
>
> </FpML>
>
>
> Here's what I hope are the pertinent parts of my code so far:
>
> FpmlFactory factory = FpmlFactory.eINSTANCE; Trade
> trade = factory.createTrade(); CreditDefaultSwap cds =
> factory.createCreditDefaultSwap();
> cds.setCdsName("Toby CDS");
> FeatureMap featureMap = trade.getProductGroup();
> featureMap.add( TobyPackage.Literals.TRADE__PRODUCT, cds);
Maybe product is abstract...
> / / -> java.lang.RuntimeException: Invalid entry feature
> 'Trade.product'
>
> // code would then go on to save the resource which is fine
>
>
> My understanding is that for substition groups, I need to use a
> FeatureMap. I'm not sure I've understood quite how these work though,
> as the above code is throwing an exception as a per the comment in the
> code. I've looked at the EMF book and saw a bit about mixed
> featureMaps which might be relevant but I couldn't quite get my head
> around it:
>
> Quote:
>> If the reference to a head or abstract element is nested within a
>> schema component for which a feature map would already be produced
>> (e.g., if the containing complex type is mixed), the resulting
>> feature map EAttribute then derives from that other feature map.
>
>
> I've massively simplified the XSD schema to extract the problem parts,
> the two XSD files are at the end of this post.
>
> Can someone advise where I'm going wrong please? I've tried this on
> the full FpML model too with the same result (Invalid entry feature
> 'Trade.product')
>
> test.xsd:
>
> <?xml version="1.0" encoding="utf-8"?>
> <xsd:schema xmlns="http://www.fpml.org/2010/FpML-4-8"
> xmlns:fpml="http://www.fpml.org/2010/FpML-4-8"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
> ecore:nsPrefix="fpml" ecore:package="org.fpml"
> ecore:documentRoot="FpML"
> targetNamespace="http://www.fpml.org/2010/FpML-4-8"
> attributeFormDefault="unqualified" elementFormDefault="qualified">
>
> <xsd:include schemaLocation="test-shared.xsd" />
>
> <xsd:element name="FpML" type="Document"/>
>
> </xsd:schema>
>
>
>
> and
> test-shared.xsd
>
> <?xml version="1.0" encoding="utf-8"?>
>
> <xsd:schema xmlns="http://www.fpml.org/2010/FpML-4-8"
> targetNamespace="http://www.fpml.org/2010/FpML-4-8"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
> ecore:nsPrefix="fpml" ecore:package="org.fpml"
> ecore:documentRoot="FpML" elementFormDefault="qualified"
> attributeFormDefault="unqualified">
> <xsd:complexType name="Document" abstract="true"/>
> <xsd:complexType name="DataDocument"> <xsd:complexContent>
> <xsd:extension base="Document">
> <xsd:sequence> <xsd:choice>
> <xsd:sequence>
> <xsd:element name="trade" type="Trade" minOccurs="0"
> maxOccurs="unbounded">
> </xsd:element> </xsd:sequence>
> </xsd:choice> </xsd:sequence>
> </xsd:extension>
> </xsd:complexContent>
> </xsd:complexType>
>
> <xsd:complexType name="Trade"> <xsd:sequence>
> <xsd:element ref="product" />
> </xsd:sequence>
> </xsd:complexType>
>
> <xsd:element name="product" type="Product" abstract="true"/>
Yep, abstract...
> <xsd:complexType name="Product" abstract="true"/>
> <xsd:element name="creditDefaultSwap" type="CreditDefaultSwap"
> substitutionGroup="product"/> <xsd:complexType
> name="CreditDefaultSwap">
> <xsd:complexContent>
> <xsd:extension base="Product">
> <xsd:sequence>
> <xsd:element name="cdsName" type="xsd:string"/>
> </xsd:sequence>
> </xsd:extension> </xsd:complexContent>
> </xsd:complexType>
>
> </xsd:schema>
>
>
>
> Many thanks in advance to anyone who can help, I've really hit a brick
> wall with this.
I think you want to use

featureMap.add(
TobyPackage.Literals.DOCUMENT_ROOT__CREDIT_DEFAULT_SWAP, cds);

I.e., use the feature corresponding to a non-abstract element in the
substitution group.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: XML substitution groups and FeatureMaps [message #646049 is a reply to message #646044] Tue, 21 December 2010 17:38 Go to previous message
TobyS is currently offline TobySFriend
Messages: 4
Registered: July 2009
Junior Member
Brilliant thanks a lot Ed that's worked a treat.

More stupid questions likely to follow later Smile
Previous Topic:URI Mapper example - resolve proxy: platform:/model
Next Topic:Inheritance between to model plugins
Goto Forum:
  


Current Time: Fri Apr 26 07:51:57 GMT 2024

Powered by FUDForum. Page generated in 0.02539 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top