Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » From XSD to Xtext: xsd:any / arbitrary XML content
From XSD to Xtext: xsd:any / arbitrary XML content [message #1696134] Fri, 22 May 2015 09:38
Patrick Neubauer is currently offline Patrick NeubauerFriend
Messages: 8
Registered: December 2014
Junior Member

I am wondering how to transform a given XSD to Xtext grammar and allow to serialize XMLs to Xtext models and vice-versa including arbitrary XML content using xs:any or similar.

To approach this, I am using both the "XSD to Ecore Importer" and "Xtext Project from existing Ecore Models" Eclipse wizards to transform a given XSD into Xtext grammar.

Furthermore, I build a Java class that employs the standalone setup of the Xtext project that has been generated for the Ecore model. It uses the following lines of code to read an XML conforming to the XSD and save it in the format of the Xtext DSL:
// load input model from file
Resource inputResource = resourceSet.getResource(inputModelURI, true);

// create output model resource by copying contents of input model
Resource outputResource = resourceSet.createResource(outputModelURI);
// save output model to file;

This works quite well for simple XSDs - even a roundtrip, i.e., from XML to mydsl and (back) to XML, however as soon as constructs like xsd:any or xsd:element with type="xsd:anyType" are introduced, that allow to represent any arbitrary XML content including any attributes and mixed text in an instance, things become fuzzy - let me explain in more detail.

Let's say we have the following simplistic XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd=""
	xmlns:ecore="" targetNamespace=""

	<!-- ROOT ELEMENT -->
	<xsd:element name="rootElement">
				<xsd:element name="properties" type="Properties" minOccurs="0" />
	<xsd:complexType name="Properties">
			<xsd:any namespace="##other" processContents="lax" />


The above XSD allows us to represent an XML like the following:
<?xml version="1.0" encoding="UTF-8"?>
<p:rootElement xmlns:p=""
	xsi:schemaLocation=" ../metamodels/Example_with_anyfeature.xsd ">
		<unknownElement1 xmlns="">

NOTE that we can place arbitrary XML content inside the "properties" element.

If we transform the XSD to an Ecore metamodel using the XSD to Ecore Importer we get the following metamodel:
NOTE that the "any" property inside the EClass "Properties" is of type "EFeatureMapEntry" (i.e., org.eclipse.emf.ecore.util.FeatureMap$Entry, to be specific).

If this Ecore metamodel is used to build Xtext grammar, the following grammar comes out of the "Xtext Project from existing Ecore Models" wizard (using the "RootElementType" as entry rule):
// automatically generated by Xtext
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

import "" 
import "" as type
import "" as ecore

RootElementType returns RootElementType:
		('properties' properties=Properties)?

Properties returns Properties:

NOTE that the "Properties" grammar rule contains no corresponding construct to store values for the EFeatureMapEntry.

When using the above serialization mechanism the above XML causes a "FeatureNotFoundException: Feature 'unknownElement1' not found".

Here the question arises what are the options that allow representing the content depicted in the above XML file and to allow roundtrip instance transformations (i.e., from XML to Xtext model and vice-versa) that keep such arbitrary XML content in place.

Attached you find the projects containing the above files.

Cheers, Patrick
Previous Topic:Correct way of migration from old version to new one
Next Topic:Generation error after upgrading xtext
Goto Forum:

Current Time: Thu Apr 22 18:25:14 GMT 2021

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

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

Back to the top