Thursday, 19 February 2015

Handling Namespaces and Imports when reading XML using EMF Ecore



Since I could not already find an answer by search I post this issue here.


At the metamodel level: In my scenario I created a Genmodel and Ecore file starting from an XML schema in Eclipse by using the wizard "Eclipse Modeling Framework > EMF Generator Model". Next, I used again a wizard "Xtext Project from Existing Ecore Models" to create an Xtext project containing grammar among other things. Furthermore, by right clicking on the root element of the Genmodel I was able to "Generate All" (i.e., code for models, editor, edit, and test).


At the model level: Here I have an XML conforming to the XML schema that has been used to create all the above mentioned metamodel artifacts. Now, I created a simple serializer that registers my customized DSL, reads the XML into a org.eclipse.emf.ecore.resource.Resource, and then stores it either as an XMI or in the format of my customized DSL. This works well - now let's describe the issue.


Issue: As soon as there are other namespaces opened up inside the XML file I get the following FeatureNotFoundException:



org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'MyElement' not found.


The XML, containing MyElement looks like this:





<?xml version="1.0" encoding="UTF-8"?>
<base:DocumentRoot xmlns:base="http://ift.tt/1AnKwYA">
<definitions id="MyDefinitions" name="My Definitions" targetNamespace="http://ift.tt/1MzLxmB" xmlns="http://ift.tt/1AnKwYA" xmlns:ns1="http://ift.tt/1AnKvDX">
<import importType="http://ift.tt/1AnKwYA" location="MyTypes-Definitions.xml" namespace="http://ift.tt/1MzLAir" />
<baseElement id="My Example" name="My Example">
<properties>
<ns1:MyElement xmlns:ns1="http://ift.tt/1AnKwYC" xmlns="http://ift.tt/1AnKwYC">
<Attribute1>1</Attribute1>
<Attribute2>123</Attribute2>
</ns1:MyElement>
</properties>
</baseElement>
</definitions>
</base:DocumentRoot>



Do I miss some step? How to handle namespaces and import correctly?


What I have already tried is:



  • Loading the XML defining MyElement into the ResourceSet prior to loading the above listed XML

  • Registering the Ecore metamodel corresponding to the XML schema of the XML file containing MyElement


Any help is very much appreciated.


No comments:

Post a Comment