Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » selecting the "highest version" of an xml element for a jet2 transform
selecting the "highest version" of an xml element for a jet2 transform [message #37814] Tue, 11 December 2007 15:43 Go to next message
Edoardo Comar is currently offline Edoardo ComarFriend
Messages: 102
Registered: July 2009
Senior Member
My input model for a jet2 transform has a format like the following
<root>
<element name="foo">...</element>
<element name="foo" version="100">...</element>
<element name="bar">...</element>
<element name="baz">...</element>
<element name="baz" version="100">...</element>
<element name="baz" version="200">...</element>
<root>

where each element *may* exist multiple times distinguished by a version
attribute
I need to iterate over the elements, but for each name only generate
stuff for the element with the highest version (the one without version
is the lowest version so it has to be discarded if it's not the only one).

Is this doable ??
Re: selecting the "highest version" of an xml element for a jet2 transform [message #37847 is a reply to message #37814] Wed, 12 December 2007 17:37 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Edoardo:

Unfortunately, there is no built-in XPath function to do this. You could, of
course write on, but you can also resort to more normal programming
techniques...

Do this in you main.jet:

<%-- Create an attribute isMostRecentVersion on all 'element' elements. Set
to either 'yes' or 'no' --%>
<%-- Assumptions:
* elements with identical names are in sequence
* versions may be in any order in the sequence
* a missing version tag is equivalent to the original version
(i.e. the lowest possible version number)
--%>
<c:setVariable var="currentName" select=" '' "/>
<c:iterate select="/root/element" var="element">
<c:choose>
<c:when test="not( $element/@name = $currentName )">
<c:setVariable var="currentName" select=" $element/@name "/>
<%-- make first $element the most recent version (temporarily) --%>
<c:set select="$element" name="isMostRecentVersion">yes</c:set>
<c:setVariable var="curMax" select=" $element "/>
</c:when>
<c:when test="not($curMax/@version) or number($element/@version) >
number($curMax/@version)" >
<%-- Make $element the most recent version --%>
<c:set select="$element" name="isMostRecentVersion">yes</c:set>
<c:set select="$curMax" name="isMostRecentVersion">no</c:set>
<c:setVariable var="curMax" select=" $element "/>
</c:when>
<c:otherwise>
<c:set select="$element" name="isMostRecentVersion">no</c:set>
</c:otherwise>
</c:choose>
</c:iterate>

Then, in your templates, or in the rest of main.jet, you can do:

<c:iterate select="/root/element[@isMostRecentVersion = 'yes'] var="...">
...
</c:iterate>

Paul

"Edoardo Comar" <ecomar@uk.ibm.com> wrote in message
news:fjmb69$8tu$1@build.eclipse.org...
> My input model for a jet2 transform has a format like the following
> <root>
> <element name="foo">...</element>
> <element name="foo" version="100">...</element>
> <element name="bar">...</element>
> <element name="baz">...</element>
> <element name="baz" version="100">...</element>
> <element name="baz" version="200">...</element>
> <root>
>
> where each element *may* exist multiple times distinguished by a version
> attribute
> I need to iterate over the elements, but for each name only generate stuff
> for the element with the highest version (the one without version is the
> lowest version so it has to be discarded if it's not the only one).
>
> Is this doable ??
Re: selecting the "highest version" of an xml element for a jet2 transform [message #37879 is a reply to message #37847] Wed, 12 December 2007 19:05 Go to previous message
Edoardo Comar is currently offline Edoardo ComarFriend
Messages: 102
Registered: July 2009
Senior Member
perfect! thanks Paul !!!!!!!!!!!!!!!!


Paul Elder wrote:
> Edoardo:
>
> Unfortunately, there is no built-in XPath function to do this. You could, of
> course write on, but you can also resort to more normal programming
> techniques...
>
> Do this in you main.jet:
>
> <%-- Create an attribute isMostRecentVersion on all 'element' elements. Set
> to either 'yes' or 'no' --%>
> <%-- Assumptions:
> * elements with identical names are in sequence
> * versions may be in any order in the sequence
> * a missing version tag is equivalent to the original version
> (i.e. the lowest possible version number)
> --%>
> <c:setVariable var="currentName" select=" '' "/>
> <c:iterate select="/root/element" var="element">
> <c:choose>
> <c:when test="not( $element/@name = $currentName )">
> <c:setVariable var="currentName" select=" $element/@name "/>
> <%-- make first $element the most recent version (temporarily) --%>
> <c:set select="$element" name="isMostRecentVersion">yes</c:set>
> <c:setVariable var="curMax" select=" $element "/>
> </c:when>
> <c:when test="not($curMax/@version) or number($element/@version) >
> number($curMax/@version)" >
> <%-- Make $element the most recent version --%>
> <c:set select="$element" name="isMostRecentVersion">yes</c:set>
> <c:set select="$curMax" name="isMostRecentVersion">no</c:set>
> <c:setVariable var="curMax" select=" $element "/>
> </c:when>
> <c:otherwise>
> <c:set select="$element" name="isMostRecentVersion">no</c:set>
> </c:otherwise>
> </c:choose>
> </c:iterate>
>
> Then, in your templates, or in the rest of main.jet, you can do:
>
> <c:iterate select="/root/element[@isMostRecentVersion = 'yes'] var="...">
> ...
> </c:iterate>
>
> Paul
>
> "Edoardo Comar" <ecomar@uk.ibm.com> wrote in message
> news:fjmb69$8tu$1@build.eclipse.org...
>> My input model for a jet2 transform has a format like the following
>> <root>
>> <element name="foo">...</element>
>> <element name="foo" version="100">...</element>
>> <element name="bar">...</element>
>> <element name="baz">...</element>
>> <element name="baz" version="100">...</element>
>> <element name="baz" version="200">...</element>
>> <root>
>>
>> where each element *may* exist multiple times distinguished by a version
>> attribute
>> I need to iterate over the elements, but for each name only generate stuff
>> for the element with the highest version (the one without version is the
>> lowest version so it has to be discarded if it's not the only one).
>>
>> Is this doable ??
>
>
Previous Topic:[JET] Re: Possible noob issue with xsi:type
Next Topic:Getting a variable which name is the content of another.
Goto Forum:
  


Current Time: Thu Apr 25 06:06:33 GMT 2024

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

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

Back to the top