Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] How to sort elements...?

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Manfred Lotz <manfred.lotz AT arcor.de>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] How to sort elements...?
  • Date: Mon, 05 May 2008 17:29:01 +0200

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





Archive powered by MHonArc 2.6.24.

Top of Page