Re: sorting xml nodes in freemarker

Daniel López <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Hi there,

FWIW, we use a framework that's based around XML and we have the option 
of using XSLT/FreeMarker/JSP to process it. FreeMarker is fine if you 
want to produce "raw text", but XML processing is a bit convoluted in 
FreeMarker and is limited, so if you want to produce XML or you need 
some advanced XML processing, I would recommend XSLT, which has its own 
little quircks.
This is not to say someting against FreeMarker, but it was not created 
specifically for this purpose and it is not where it shines and it is 
reasonably limited (one piece of software can't be good for eveything).

We do combine both kinds of processing and we are quite happy with it 
(JSP we just use when forced to, like when doing portlet development, 
yuk! :()

My 2ec,
D.


Daniel Dekany escribió:
> Thursday, February 4, 2010, 2:41:17 AM, Kunal wrote:
> 
>> Hi Daniel,
>>
>> Thanks for the reply. That really helped.
>>
>> However, my requirement is some thing like this. Consider the xml below
>>
>> <games>
>>     <game name = "b">
>>           <publisher>
>>                <gameCompany name = "abc"></gameCompany>
>>           </publisher>
>>           <rating>9.0</rating>
>>      </game>
>>     <game name = "a">
>>           <publisher>
>>                <gameCompany name = "pqr"></gameCompany>
>>           </publisher>
>>      </game> 
>>     <game name = "c">
>>           <publisher>
>>                <gameCompany name = "xyz"></gameCompany>
>>           </publisher>
>>           <rating>6.0</rating>
>>     </game>
>> </games> 
>>
>>
>> I want to sort the game nodes based on the gameCompany name. How do I write
>> the syntax?
> 
> Assuming doc stores the XML document:
> 
>   <#list doc.games.game?sort_by(['publisher', 'gameCompany', '@name']) as g>
>     ${g.@@markup}
>   </#list>
> 
>> Also, Suppose I want to sort on rating , when I write  
>>
>> [#list game_root.game?sort_by("rating") as game]
>>
>> I get the following error
>> sort_by(...) failed: Values used for sorting must be numbers, strings, or
>> date/time values.
>> I suppose this error is because in the second game node rating node does not
>> exist.
> 
> Yes, that's a problem (however, if you provide the default attribute
> value with DTD then it may goes away... but I never tried that).
> Another problem is that sorting numbers is not the same as sorting the
> string representations of numbers, and XML stores text (OK, not
> anymore with W3C Schemas, but FreeMarker doesn't know that). Like
> "10.0" (the string) is less than "9.0".
> 
>> Is there any way I can still have the sorting done based on rating
>> without changing the xml?
> 
> Unfortunately, ?sort_by is not powerful enough for this: it can't be
> instructed to convert string values to numbers, and doesn't provide
> default value for missing items. However, even the existence of
> ?sort/?sort_by is seen as something that's to powerful and thus
> encourages bad practices... FreeMarker assumes that the data-model
> contains the data already calculated, and that it only have to deal
> with presentation issues. Sorting is mostly not that. Of course, if
> you want to use FreeMarker to transform XML to XML, that's a different
> story... but I never agreed with Jonathan in that FreeMarker should be
> advertised as an XML transforming tool. It has some built-in features
> to traverse XML in the data-model, but it still was made to and
> remained an MVC-centric template engine.
> 
> Anyway... what can you do? Well, you could write a
> TemplateMethodModelEx that does what you need (sort the tree nodes in
> this and that way). (Or we should improve ?sort_by, but that certainly
> won't happen soon if ever, as there is a lot of higher priority issues
> piled up. Unless, you do it, test it, and sign the CLA.) Or, you could
> preprocess the DOM tree before passing it to FreeMarker. Of course, if
> you just wanted to transform XML to XML, these doesn't make much
> sense... I can only point back to my statement that it's not in
> general an XML transforming tool. /-:
> 
>> Thanks.
>> Kunal



------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.