Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » XML extraction problem
XML extraction problem [message #47616] Tue, 15 July 2008 12:15 Go to next message
Eclipse User
Originally posted by: mats.bengtar.saabgroup.com

Hi,

I have a problem extracting the information I want from my EMF-model. I
have created a meta-Model and from the model (.cim) I want to generate
code in the form of .mof (Managed Object Format). I therefore use XPath in
JET to extract information from the model and print it the way I like. But
I can't seem to get the information I want...hopefully you can help me.

This is my simple CIM-model expressed in XML:

<?xml version="1.0" encoding="UTF-8"?>
<saabgroup:CModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:saabgroup="http://saabgroup.com/cim">
<namespace>
<classes xsi:type="saabgroup:CClass" name="aClass">
<qualifiers xsi:type="saabgroup:CClassConstraint"
name="ClassConstraint">
<value>one</value>
<value>two</value>
</qualifiers>
<qualifiers xsi:type="saabgroup:CAbstract" name="Abstract"
subclass="Restricted"/>
<qualifiers xsi:type="saabgroup:CDescription" name="Description"
value="Desc.">
<translatable/>
</qualifiers>
</classes>
</namespace>
</saabgroup:CModel>


I want to extract the second value='two' from my first qualifier. Of what
I have understood from XPath I tried the following:
<c:get select="/CModel/namespace/classes/qualifiers[1]/value[1]/text() ">
But no result is returned. I ahve also tried many similar variants but
everyone including text() fails.

I did manage to get the first value from:
<c:get select="/CModel/namespace/classes/qualifiers[1]/@value">
But this implies that 'value' would be an attribute in the XML code, and I
still can't get the second value. In the meta model value is expressed as
a list of strings (For the qualifier of the type Description it is just a
string).

Another thing I'm wondering is from where exactly I get the information
when I make reference to myCimModel.cim? Since I can extract information
not expressed in the XML file it all seems much more complicated than just
reading from an XML file.

Best regards
-Mats
Re: XML extraction problem [message #47707 is a reply to message #47616] Wed, 16 July 2008 14:59 Go to previous messageGo to next message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Mats:

Looking at your model, it appears to have been created via EMF's XMI
serialization. Am I correct?

I'll answer the rest of your e-mail in two ways:

1) Your model is created by EMF:

JET is uses EMF's reflective interfaces on the loaded EObjects to resolve
XPath expressions and not the XML that you see in the file.

With respect to:

<c:get select="/CModel/namespace/classes/qualifiers[1]/value[1]/text() ">

How is 'value' declared on the EClass CClass? My guess is that it is as an
unbounded String array? If so, /CModel/namespace/classes/qualifiers/@value
will get you the first value of the array. However, you will have trouble
getting the other values. I have bug 208977 (http://bugs.eclipse.org/208977)
open. For a solution, I'm thinking of allowing something like:
/CModel/namespace/classes/qualifiers/@value[2].

Let me know if you are interested.

2) Your model is not EMF based, or you want to deal directly with the XML
inspite of it being EMF-based.

Open plugin.xml, go to the Extensions tab, expand org.eclipse.jet.transform
and select (transform). Set the modelLoader field to 'org.eclipse.jet.xml'.

This will cause the document to be loaded using the XML DOM instead of via
EMF. Then the XPath engine will traverse the DOM itself.

One note on the XMI created by EMF's serialization. The XMI serialization
omits any attributes whose value is equal to the default specified in the
meta-model. This means that your transformation must know what those default
values are.

Paul


"Mats" <mats.bengtar@saabgroup.com> wrote in message
news:c97f32fb795b360fa7be5b1585538df5$1@www.eclipse.org...
> Hi,
>
> I have a problem extracting the information I want from my EMF-model. I
> have created a meta-Model and from the model (.cim) I want to generate
> code in the form of .mof (Managed Object Format). I therefore use XPath in
> JET to extract information from the model and print it the way I like. But
> I can't seem to get the information I want...hopefully you can help me.
>
> This is my simple CIM-model expressed in XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <saabgroup:CModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:saabgroup="http://saabgroup.com/cim">
> <namespace>
> <classes xsi:type="saabgroup:CClass" name="aClass">
> <qualifiers xsi:type="saabgroup:CClassConstraint"
> name="ClassConstraint">
> <value>one</value>
> <value>two</value>
> </qualifiers>
> <qualifiers xsi:type="saabgroup:CAbstract" name="Abstract"
> subclass="Restricted"/>
> <qualifiers xsi:type="saabgroup:CDescription" name="Description"
> value="Desc.">
> <translatable/>
> </qualifiers>
> </classes>
> </namespace>
> </saabgroup:CModel>
>
>
> I want to extract the second value='two' from my first qualifier. Of what
> I have understood from XPath I tried the following:
> <c:get select="/CModel/namespace/classes/qualifiers[1]/value[1]/text() ">
> But no result is returned. I ahve also tried many similar variants but
> everyone including text() fails.
> I did manage to get the first value from:
> <c:get select="/CModel/namespace/classes/qualifiers[1]/@value">
> But this implies that 'value' would be an attribute in the XML code, and I
> still can't get the second value. In the meta model value is expressed as
> a list of strings (For the qualifier of the type Description it is just a
> string).
>
> Another thing I'm wondering is from where exactly I get the information
> when I make reference to myCimModel.cim? Since I can extract information
> not expressed in the XML file it all seems much more complicated than just
> reading from an XML file.
>
> Best regards
> -Mats
>
Re: XML extraction problem [message #47788 is a reply to message #47707] Thu, 17 July 2008 07:51 Go to previous messageGo to next message
Eclipse User
Originally posted by: mats.bengtar.saabgroup.com

Thanks Paul.

You are correct, my model is EMF-based. But your second alternative is
also very helpful, since I would find it easier to deal directly with the
XML. But to the problem. Value is declared as a string array (i.e. an
unbounded list of strings). So what is there to do next? Have you found a
solution to get around this issue or is it just to wait for this 'bug' to
get resolved?

Best regards
Mats
Re: XML extraction problem [message #47938 is a reply to message #47788] Thu, 17 July 2008 13:31 Go to previous messageGo to next message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Mats:

I am considering a 'fix'. My mental block has been the XPath spec, which
doesn't talk about attribute arrays because they do not exist in XML. But,
more generally, the XPath spec allows a numeric predicate expression after
any step, including an attribute step, so, I think I can interpret

$cClass/@value

would refer to the entire collection of objects in the array.

and @cClass/@value[n] would index the array (1-based, because XPath uses
one-based indices).

In theory, this is a simple change. In practice, I am worried that there may
be deep rooted assumptions that attributes are single valued.

Expect a fix in JET 0.9.1 (Ganymede SR1 at the end of September). If you add
yourself as a CC to the defect, you'll get notifications as to when the fix
gets committed and which integration build it is in.

Here's a link to the bug: http://bugs.eclipse.org/208977

Paul
Re: XML extraction problem [message #47998 is a reply to message #47707] Tue, 22 July 2008 09:56 Go to previous messageGo to next message
Eclipse User
Originally posted by: mats.bengtar.saabgroup.com

Paul,

It doesn't seem to work to set the modelLoader to 'org.eclipse.jet.xml'.
It still doesn't read the information from just the xml, because I still
get information that isn't there. Are you sure that is the way to set the
input?

Best regards
Mats Bengtar
Re: XML extraction problem [message #48028 is a reply to message #47998] Tue, 22 July 2008 13:41 Go to previous messageGo to next message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Mats:

The problem is with the initial step of the XPath expression:

/CModel

Recall that your model looks like this:

<saabgroup:CModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:saabgroup="http://saabgroup.com/cim">
....

It turns out that in XPath /CModel does not match saabgroup:CModel.

XPath allows you to write expressions like:

/saabgroup:CModel

but, JET currently has not way to define what the prefix 'saabgroup' maps to
(as a namespace URI).

There are a number of work arounds, most of which involve defining an XPath
variable for the root element:

<c:setVariable var="cmodel" select="....">
<c:get select="$cmodel/namespace/classes/qualifiers[1]/value[1]/text() " />

Here are the possible values for the select expression in setVariable:

1) /* - if you know for sure what the domain of the input will be, then this
is by far the easiest.
2) /*[local-name() = 'CModel']. This would match a root element, so long as
the local name (the part after the prefix) is 'CModel'.
2) /*[local-name() = 'CModel' and namespace-uri() =
'http://saabgroup.com/cim']. This would match only root elements that
confirm to the given schema. This is the most strict.

Ultimately, I'd like to define a tag something like this:

<c:namespace prefix="saabgroup" uri="http://saabgroup.com/cim"/>

Then, you could write:

<c:get select="/saabgroup:CModel/..." />

I have a bugzilla for this
(https://bugs.eclipse.org/bugs/show_bug.cgi?id=184692), but I haven't gotten
around to it yet.

Paul
Re: XML extraction problem [message #48057 is a reply to message #48028] Wed, 23 July 2008 07:27 Go to previous messageGo to next message
Eclipse User
Originally posted by: mats.bengtar.saabgroup.com

Ok, thanks. I think you misinterpreted my question, or I just didn't
understand your answer. I want to read directly from the xml-file.

Lets say I have a value 'subclass' for each qualifier. The value is
choosen as an enumeration, the options are "ToSubclass" and "Restricted".
The default value is set to ToSubclass.

So if I then write <c:get select="$qualifiers[1]/@subclass"/> I will get
Restricted returned if the value has been changed to this in the model or
I will get nothing returned since default values are not written in the
xml-file. Is this possible to configure?

Best regards
Mats Bengtar
Re: XML extraction problem [message #48087 is a reply to message #48057] Wed, 23 July 2008 16:51 Go to previous messageGo to next message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Mats:

<c:get select="$qualifiers[1]/@subclass"/>

will return a value if the @subclass attribute is there. Otherwise, it will
report an error.

If you want a value returned when @subclass is not defined, use:

<c:get select="$qualifiers[1]/@subclass" default="..."/>

Paul
Re: XML extraction problem [message #48116 is a reply to message #48087] Thu, 24 July 2008 08:33 Go to previous messageGo to next message
Eclipse User
Originally posted by: mats.bengtar.saabgroup.com

Paul,
It doesn't work for me. I have set:
modelLoader: org.eclipse.jet.xml
modelExtension: xml

It doesn't read from just from the xml. If I do...
<c:get select="$qualifiers[1]/@subclass"/>
...I get a value returned even if it isn't changed from default. Now, the
problem might be that subclass is an enumeration so it is always set to a
value != null.

I have experienced that it reports an error for e.g. a string, since the
default is set to null. So my conclusion is that it's not taking info just
from the xml, even though it might seem so when working with strings.

Best regards
Mats
Re: XML extraction problem [message #48145 is a reply to message #48116] Thu, 24 July 2008 11:47 Go to previous messageGo to next message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Mats:

What version of Eclipse and JET are you using.

Using the example you provided in your first message, I evaluated the
following loop:

<c:iterate select="$cModel/namespace/classes/qualifiers" var="qualifier">
Qualifier: <c:get select="$qualifier/@name"/>. subclass = <c:get
select="$qualifier/@subclass" default="--- Not Set ---"/>
</c:iterate>

and got these results...

Qualifier: ClassConstraint. subclass = --- Not Set ---
Qualifier: Abstract. subclass = Restricted
Qualifier: Description. subclass = --- Not Set ---

Which is what I would expect.

The only other difference between our environments is that I do not have
the XML schema for http://saabgroup.com/cim

Are you able to post your entire test template?

Paul
Re: XML extraction problem [message #48184 is a reply to message #48145] Fri, 25 July 2008 08:34 Go to previous message
Eclipse User
Originally posted by: mats.bengtar.saabgroup.com

Thanks for all your help!
Unfortunately I am leaving this issue. One of my colleagues will
investigate your recommendations further at some point after the summer.
-Mats
Previous Topic:[MOFScript] Problem with metamodel setting in Java integration
Next Topic:Load UML model in JET
Goto Forum:
  


Current Time: Tue Sep 16 21:30:59 GMT 2014

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

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