Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Problem XSD to ECore mapping(How to define XSD to not get attribute "value" in XML output)
Problem XSD to ECore mapping [message #869810] Fri, 04 May 2012 11:37 Go to next message
F. Sola is currently offline F. SolaFriend
Messages: 3
Registered: May 2012
Junior Member
Hello,
the short description:

Using of XSD with EMF I get an xml output
<time_of_day value="23:54"/>
but I want to get
<time_of_day>23:54</time_of_day>

Question: How the XSD must look like?

In detail:

Environment: Eclipse 3.7 Indigo
In short the doing in an Eclipse EMF Project, for example named emf_party:
1. I use an XSD (party.xsd)
2. I import the XSD in the EMF Projekt (=> party.genmodel and Party.ecore will be generated)
3. I let generate from model the Java Code.
4. I use the Java Code to fill elements and to write the result xml file.

Now going to the problem:

In output I get:
<time_of_day value="23:54"/>

But I want to get:
<time_of_day>23:54</time_of_day>


Question:
How the XSD must look like?

The sources
party.xsd:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:ecore="www.eclipse.org/emf/2002/Ecore" xmlns:xsd="www . w3.org/2001/XMLSchema" elementFormDefault="qualified" ecore:nsPrefix="Party" ecore:package="Party">
<xsd:simpleType name="dateType" ecore:name="DatumType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[03][09].[01][09].[09][09]"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="guestType" ecore:name="GuestType">
<xsd:sequence>
<xsd:element name="drink" type="xsd:string"/>
<xsd:element name="state" type="stateType"/>
<xsd:element name="time_of_day">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:anyURI"/>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="partyType" ecore:name="PartyType">
<xsd:sequence>
<xsd:element name="guest" type="guestType"/>
</xsd:sequence>
<xsd:attribute name="date" type="dateType"/>
</xsd:complexType>
<xsd:complexType name="stateType" ecore:name="StateType">
<xsd:attribute name="single" type="xsd:boolean"/>
<xsd:attribute name="dry" type="xsd:boolean"/>
</xsd:complexType>
</xsd:schema>

The java code
private void createPartyXml() {
PartyFactoryImpl.init();
PartyFactory factory = PartyFactory.eINSTANCE;
PartyType party = factory.createPartyType();
party.setDate("21.12.2112");
GuestType guest = factory.createGuestType();
guest.setDrink("beer");
StateType state = factory.createStateType();
state.setSingle(true);
state.setDry(false);
guest.setState(state);
party.setGuest(guest);
TimeOfDayType time_of_day = factory.createTimeOfDayType();
time_of_day.setValue("23:54");
guest.setTimeOfDay(time_of_day);
final Resource res = new XMLResourceImpl();
res.getContents().add(party);
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream("party_result_en.xml");
res.save(outputStream, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

result xml:

<?xml version="1.0" encoding="ASCII"?>
<Party:PartyType xmlns:Party="platform:/resource/emf_party_en/model/Party.xsd" date="21.12.2112">
<guest drink="beer">
<state dry="false" single="true"/>
<timeOfDay value="23:54"/>
</guest>
</Party:PartyType>

Regards Farisola
Re: Problem XSD to ECore mapping [message #869846 is a reply to message #869810] Fri, 04 May 2012 13:45 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Comments below.

On 04/05/2012 1:37 PM, F. Sola wrote:
> Hello,
> the short description:
>
> Using of XSD with EMF I get an xml output <time_of_day value="23:54"/>
> but I want to get <time_of_day>23:54</time_of_day>
>
> Question: How the XSD must look like?
It sounds like you have an XSD already though?
>
> In detail:
>
> Environment: Eclipse 3.7 Indigo
> In short the doing in an Eclipse EMF Project, for example named
> emf_party:
> 1. I use an XSD (party.xsd)
> 2. I import the XSD in the EMF Projekt (=> party.genmodel and
> Party.ecore will be generated)
> 3. I let generate from model the Java Code.
> 4. I use the Java Code to fill elements and to write the result xml
> file.
>
> Now going to the problem:
>
> In output I get:
> <time_of_day value="23:54"/>
>
> But I want to get:
> <time_of_day>23:54</time_of_day>
>
>
> Question:
> How the XSD must look like?
>
> The sources
> party.xsd:
>
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <xsd:schema xmlns:ecore="www.eclipse.org/emf/2002/Ecore"
> xmlns:xsd="www . w3.org/2001/XMLSchema" elementFormDefault="qualified"
> ecore:nsPrefix="Party" ecore:package="Party">
> <xsd:simpleType name="dateType" ecore:name="DatumType">
> <xsd:restriction base="xsd:string">
> <xsd:pattern value="[03][09].[01][09].[09][09]"/>
> </xsd:restriction>
> </xsd:simpleType>
> <xsd:complexType name="guestType" ecore:name="GuestType">
> <xsd:sequence>
> <xsd:element name="drink" type="xsd:string"/>
> <xsd:element name="state" type="stateType"/>
> <xsd:element name="time_of_day">
> <xsd:complexType>
> <xsd:simpleContent>
> <xsd:extension base="xsd:anyURI"/>
> </xsd:simpleContent>
> </xsd:complexType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> <xsd:complexType name="partyType" ecore:name="PartyType">
> <xsd:sequence>
> <xsd:element name="guest" type="guestType"/>
> </xsd:sequence>
> <xsd:attribute name="date" type="dateType"/>
> </xsd:complexType>
> <xsd:complexType name="stateType" ecore:name="StateType">
> <xsd:attribute name="single" type="xsd:boolean"/>
> <xsd:attribute name="dry" type="xsd:boolean"/>
> </xsd:complexType>
> </xsd:schema>
>
> The java code
> private void createPartyXml() {
> PartyFactoryImpl.init();
> PartyFactory factory = PartyFactory.eINSTANCE;
> PartyType party = factory.createPartyType();
> party.setDate("21.12.2112");
> GuestType guest = factory.createGuestType();
> guest.setDrink("beer");
> StateType state = factory.createStateType();
> state.setSingle(true);
> state.setDry(false);
> guest.setState(state);
> party.setGuest(guest);
> TimeOfDayType time_of_day = factory.createTimeOfDayType();
> time_of_day.setValue("23:54");
> guest.setTimeOfDay(time_of_day);
> final Resource res = new XMLResourceImpl();
You should use the generated resource factory to create your resource.
If you look at what it does, you'll see it configures the options needed
to instruction the resource implementation to respect the extended meta
data annotations in the Ecore model.

Try invoking "Generate Test Code" in Generator that opens for the
*.genmodel, and look at the generated PartyExample.java for how your
stand-alone application should be written.
> res.getContents().add(party);
> FileOutputStream outputStream;
> try {
> outputStream = new FileOutputStream("party_result_en.xml");
> res.save(outputStream, null);
> } catch (FileNotFoundException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> result xml:
>
> <?xml version="1.0" encoding="ASCII"?>
> <Party:PartyType
> xmlns:Party="platform:/resource/emf_party_en/model/Party.xsd"
> date="21.12.2112">
> <guest drink="beer">
> <state dry="false" single="true"/>
> <timeOfDay value="23:54"/>
> </guest>
> </Party:PartyType>
>
> Regards Farisola
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem XSD to ECore mapping [message #869914 is a reply to message #869846] Fri, 04 May 2012 19:31 Go to previous messageGo to next message
F. Sola is currently offline F. SolaFriend
Messages: 3
Registered: May 2012
Junior Member
Hi Ed,
thx for your hints.
No it works.
After following your hints a have changed the
method, see below, and now it works like expected.

The well working method:

private void createPartyXml() {
// Create a resource set to hold the resources.
//
ResourceSet resourceSet = new ResourceSetImpl();
// Register the appropriate resource factory to handle all file extensions.
//
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new PartyResourceFactoryImpl());
// Register the package to ensure it is available during loading.
//
resourceSet.getPackageRegistry().put
(PartyPackage.eNS_URI,
PartyPackage.eINSTANCE);
PartyFactory factory = PartyFactory.eINSTANCE;
PartyType party = factory.createPartyType();
party.setDate("21.12.2112");
GuestType guest = factory.createGuestType();
guest.setDrink("beer");
StateType state = factory.createStateType();
state.setSingle(true);
state.setDry(false);
guest.setState(state);
party.setGuest(guest);
TimeOfDayType time_of_day = factory.createTimeOfDayType();
time_of_day.setValue("23:54");
guest.setTimeOfDay(time_of_day);
URI uri = URI.createFileURI("party_result_en.xml");
Resource res = resourceSet.createResource(uri);
res.getContents().add(party);
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream("party_result_en.xml");
res.save(outputStream, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Regards Fred
Re: Problem XSD to ECore mapping [message #870467 is a reply to message #869810] Tue, 08 May 2012 11:15 Go to previous messageGo to next message
Eneko Gómez is currently offline Eneko GómezFriend
Messages: 1
Registered: May 2012
Junior Member
Hello,

I have a problem related to this.

I also generate a new EMF project from an XSD in order to use this model as an input in a m2t transformation.

I also cannot specify the text content other way than through a "value" parameter.

An piece of the source XSD:

<xs:complexType name="agentType">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="id" type="xs:integer" use="required" />
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>


What I want:
<agent id="1" >
eGov application
</agent>


What I get:
<agent id="1" value="eGov application" />


When I create a new model according the metamodel, I only can add the content text in a "value" parameter. In the .m2t file I only can access the content text if it's in the "value" parameter (I've tried to force it in the input XML source with no success). Any idea about how to solve this issue?

Thank you in advance.

Regards, Eneko
Re: Problem XSD to ECore mapping [message #870474 is a reply to message #870467] Tue, 08 May 2012 11:30 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Eneko,

Comments below.

On 08/05/2012 1:16 PM, Eneko Gómez wrote:
> Hello,
>
> I have a problem related to this.
>
> I also generate a new EMF project from an XSD in order to use this
> model as an input in a m2t transformation.
>
> I also cannot specify the text content other way than through a
> "value" parameter.
>
> An piece of the source XSD:
>
> <xs:complexType name="agentType">
> <xs:simpleContent>
> <xs:extension base="xs:string">
> <xs:attribute name="id" type="xs:integer" use="required" />
> </xs:extension>
> </xs:simpleContent>
> </xs:complexType>
>
> What I want:
> <agent id="1" >
> eGov application
> </agent>
>
> What I get:
> <agent id="1" value="eGov application" />
>
> When I create a new model according the metamodel, I only can add the
> content text in a "value" parameter. In the .m2t file I only can
> access the content text if it's in the "value" parameter (I've tried
> to force it in the input XML source with no success). Any idea about
> how to solve this issue?
I can only repeat myself... You must ensure that your generated resource
factory is registered.
>
> Thank you in advance.
>
> Regards, Eneko


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem XSD to ECore mapping [message #870583 is a reply to message #870474] Tue, 08 May 2012 20:36 Go to previous message
F. Sola is currently offline F. SolaFriend
Messages: 3
Registered: May 2012
Junior Member
Hi Eneko,
check my reply from "Fri, 04 May 2012 15:31".
Like Ed has written it is a problem of registering.
Ed wrote:
>>>
I can only repeat myself... You must ensure that your generated resource
factory is registered.
<<<

And note:
In my message from "Fri, 04 May 2012 15:31" I have had a typo:
Correct text at start of that reply must be:
>>>
.... now it works ...
NOT
... no it works ...

So in my solution a have changed my java code in that way that I now use:

private void createPartyXml() {
1 // Create a resource set to hold the resources.
2 //
3 ResourceSet resourceSet = new ResourceSetImpl();
4 // Register the appropriate resource factory to handle all file extensions.
5 //
6 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
7 (Resource.Factory.Registry.DEFAULT_EXTENSION,
8 new PartyResourceFactoryImpl());

// Register the package to ensure it is available during loading.
//
resourceSet.getPackageRegistry().put
(PartyPackage.eNS_URI,PartyPackage.eINSTANCE);

PartyFactory factory = PartyFactory.eINSTANCE;
PartyType party = factory.createPartyType();
party.setDate("21.12.2112");
GuestType guest = factory.createGuestType();
guest.setDrink("beer");
StateType state = factory.createStateType();
state.setSingle(true);
state.setDry(false);
guest.setState(state);
party.setGuest(guest);
TimeOfDayType time_of_day = factory.createTimeOfDayType();
time_of_day.setValue("23:54");
guest.setTimeOfDay(time_of_day);

9 URI uri = URI.createFileURI("party_result_en.xml");
10 Resource res = resourceSet.createResource(uri);
11 res.getContents().add(party);
try {
res.save(null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

I think the lines 3, 6, 7, and 8 are the relevant ones.
I suppose that in your java code you should use this code but you
should change the line 8. There you have to replace
"new PartyResourceFactoryImpl()" with "your" Factory Impl.

Regards Fari

Previous Topic:[xcore] qualified references (maps) in XCore
Next Topic:[EMF Query 2] Help with writing conditions
Goto Forum:
  


Current Time: Thu Apr 25 21:19:52 GMT 2024

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

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

Back to the top