[XOM-interest] How to sort elements...?

Manfred Lotz manfred.lotz at arcor.de
Mon May 5 11:29:01 EDT 2008


Adam Constabaris wrote:
> I'd like to suggest using XSLT for this job; it's less brittle and 
> easier to customize than using the XOM API directly.  You could use an 
> identity stylesheet (e.g. 
> http://www.dpawson.co.uk/xsl/sect2/identity.html) and add the following 
> template to accomplish the task:
> 
> <xsl:template match="a">
>   <xsl:for-each select="b">
>    <xsl:sort select="@key"/>
>    <xsl:apply-templates select="."/>
>   </xsl:for-each>
> </xsl:template>
> 
> ------------------
> 
> This works nicely even if you created the document you're trying to 
> transform in XOM, because the XSLT support is right there.
> 

I agree that's really nice.



> If you really want or need to do this completely via the XOM API, then 
> the most straightforward approach would is the one you're probably 
> looking for an alternative to: copy the elements of interest into a 
> java.util.List, detach/remove those elements from the parent, sort the 
> list with a java.lang.Comparator implementation, and add them back in 
> sorted order.
> 
> [ As a coding shortcut for most of the first two steps, note that 
> Element.removeChildren() returns the removed Nodes object ]
> 

In my case I have a special comparator so that I have to go this way.


-- 
Thanks,
Manfred



More information about the XOM-interest mailing list