commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jason Horman" <ja...@jhorman.org>
Subject [jelly] xpath sorting
Date Tue, 26 Nov 2002 05:58:09 GMT
I am attaching changes to ForEachTag and SetTag to support sorting. You may
have a better idea of how this should be implemented but I thought I would
send along my version anyhow.

The XPathComparator tries to determine if the strings being sorted are
numeric or not. You can force a type by specifiying the "sortDataType"
attribute, there is an example of this below. Descending sort is supported
via a "descending" attribute.

Also attached is a "xml:sort" tag which can sort lists of xml elements, such
as those returned via "xml:set".

Example XML:

            <a>
                <b v="3"/>
                <b v="2"/>
                <b v="1"/>
                <b v="11"/>
                <b v="1.4"/>
                <b v="1.2"/>
            </a>

Example Jelly:

        <j:set var="result" value=""/>
        <x:forEach select="$nums/a/b" var="x" sort="@v" descending="true">
            <x:set var="num" select="$x/@v"/>
            <j:set var="result" value="${result} ${num.get(0).getText()}"/>
        </x:forEach>

Example Deeper XML:

            <a>
                <b><c><d>3<e>1</e></d></c></b>
                <b><c><d>2<e>11</e></d></c></b>
                <b><c><d>1</d></c></b>
                <b><c><d>11</d></c></b>
            </a>

Example Jelly:

        <j:set var="result" value=""/>
        <x:forEach select="$deeper/a/b" var="x" sort="c/d">
            <j:set var="result" value="${result} ${x.getStringValue()}"/>
        </x:forEach>

        <!-- test sorting as strings instead of auto-detecting numbers -->
        <j:set var="result" value=""/>
        <x:forEach select="$nums/a/b" var="x" sort="@v"
sortDataType="java.lang.String">
            <x:set var="num" select="$x/@v"/>
            <j:set var="result" value="${result} ${num.get(0).getText()}"/>
        </x:forEach>

-jason horman
 jason@jhorman.org
 jhorman@musicmatch.com

Mime
View raw message