sorting and filtering oh my!

Discussion in 'XML' started by snafu7x7, Oct 2, 2007.

  1. snafu7x7

    snafu7x7 Guest

    Hope someone in here can help me with this one, its got me stumped.

    I have XML data for a CD collection and a stylesheet that I have setup
    to sort it as need be (i.e. by artist, by album name, etc). However,
    since the library is rather large I want the stylesheet to be able to
    accept parameters for a range of items to return (say the first 100,
    or the last 50, or whatever). How can I accomplish this in conjunction
    with my sorting?

    Here is some sample XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <library>
    <album>
    <ID>1234</ID>
    <Name><![CDATA[Paul's Boutique]]></Name>
    <Artist><![CDATA[The Beastie Boys]]></Artist>
    <Genre><![CDATA[Rap/Hip-Hop]]></Genre>
    <Rating>8.5</Rating>

    </album>

    <album>
    <ID>5678</ID>
    <Name><![CDATA[Document]]></Name>
    <Artist><![CDATA[R.E.M]]></Artist>
    <Genre><![CDATA[Rock]]></Genre>
    <Rating>7</Rating>

    </album>

    <album>
    <ID>9988</ID>
    <Name><![CDATA[Kind Of Blue]]></Name>
    <Artist><![CDATA[Miles Davis]]></Artist>
    <Genre><![CDATA[Jazz]]></Genre>
    <Rating>8.9</Rating>

    </album>

    </library>


    And my stylesheet (note the variables I have declared for start and
    end index):

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput encoding="utf-8" method="xml" indent="yes"/>

    <xsl:param name="startIndex" select="0" />
    <xsl:param name="endIndex" select="10" />

    <xsl:template match="library">

    <xsl:copy>
    <xsl:apply-templates>
    <xsl:sort data-type="text" select="Artist" order="ascending"/>
    </xsl:apply-templates>
    </xsl:copy>

    </xsl:template>

    <xsl:template match="*">
    <xsl:copy>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>
     
    snafu7x7, Oct 2, 2007
    #1
    1. Advertising

  2. snafu7x7 wrote:
    > since the library is rather large I want the stylesheet to be able to
    > accept parameters for a range of items to return (say the first 100,
    > or the last 50, or whatever). How can I accomplish this in conjunction
    > with my sorting?


    Interesting question.

    If you look at the spec's example of numbering
    (http://www.w3.org/TR/xslt#number), you'll see that position() within
    the body of a for-each reports the position *after* sorting. So that's
    one approach -- for-each over the sorted set, test position(), and only
    apply-templates if the position is within the range you're interested in.

    There are probably a dozen other ways to accomplish this. I'd suggest
    checking the sorting section of the XSLT FAQ website -- wonderful
    resource! -- and possibly other related sections as well, to see if
    someone has contributed a more elegant solution.


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Oct 3, 2007
    #2
    1. Advertising

  3. snafu7x7

    Pavel Lepin Guest

    Joe Kesselman <> wrote in
    <>:
    > snafu7x7 wrote:
    >> since the library is rather large I want the stylesheet
    >> to be able to accept parameters for a range of items to
    >> return (say the first 100, or the last 50, or whatever).
    >> How can I accomplish this in conjunction with my sorting?

    >
    > If you look at the spec's example of numbering
    > (http://www.w3.org/TR/xslt#number), you'll see that
    > position() within the body of a for-each reports the
    > position *after* sorting. So that's one approach --
    > for-each over the sorted set, test position(), and only
    > apply-templates if the position is within the range you're
    > interested in.


    I'm probably missing your point, but the same applies to
    template matches (which seems only natural to me, since
    both xsl:for-each and xsl:apply-templates define an
    ordering on the current node list when invoked with
    xsl:sort).

    pavel@debian:~/dev/xslt$ a sortpos.xml
    <data>
    <elt-0 sort="0"/>
    <elt-1 sort="2"/>
    <elt-2 sort="7"/>
    <elt-3 sort="1"/>
    <elt-4 sort="6"/>
    <elt-5 sort="8"/>
    <elt-6 sort="3"/>
    <elt-7 sort="4"/>
    <elt-8 sort="9"/>
    <elt-9 sort="5"/>
    </data>
    pavel@debian:~/dev/xslt$ a sortpos.xsl
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="limit" select="3"/>
    <xsl:eek:utput indent="yes"/>
    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>
    <xsl:template match="data">
    <result>
    <xsl:apply-templates select="*" mode="sort-limit">
    <xsl:sort select="@sort"/>
    </xsl:apply-templates>
    </result>
    </xsl:template>
    <xsl:template match="@*|node()" mode="sort-limit">
    <xsl:if test="position() &lt;= $limit">
    <xsl:apply-templates select="."/>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
    pavel@debian:~/dev/xslt$ xsltproc sortpos.xsl sortpos.xml
    <?xml version="1.0"?>
    <result>
    <elt-0 sort="0"/>
    <elt-3 sort="1"/>
    <elt-1 sort="2"/>
    </result>
    pavel@debian:~/dev/xslt$

    --
    "You hold judicial proceedings in drinking-houses!?"
    "Poh! That judge is no more drunk than any magistrate of the
    Old Bailey."
     
    Pavel Lepin, Oct 3, 2007
    #3
  4. Pavel Lepin wrote:
    > I'm probably missing your point, but the same applies to
    > template matches (which seems only natural to me, since
    > both xsl:for-each and xsl:apply-templates define an
    > ordering on the current node list when invoked with
    > xsl:sort).


    Thought it might but hadn't checked, and depending on what the user
    wants to do that might involve using modes to distinguish the cases
    where the limit is to be checked from those where it shouldn't be.

    As I said: XSLT is a programming language, so there are generally
    multiple reasonable solutions to any "how can I" question.


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Oct 3, 2007
    #4
  5. (... Yes, I know, the "avoid for-each" philosophy. I'm not dogmatic
    about that.)

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Oct 3, 2007
    #5
  6. snafu7x7

    Pavel Lepin Guest

    Joe Kesselman <> wrote in
    <>:
    > Pavel Lepin wrote:
    >> I'm probably missing your point, but the same applies to
    >> template matches (which seems only natural to me, since
    >> both xsl:for-each and xsl:apply-templates define an
    >> ordering on the current node list when invoked with
    >> xsl:sort).

    >
    > Thought it might but hadn't checked, and depending on what
    > the user wants to do that might involve using modes to
    > distinguish the cases where the limit is to be checked
    > from those where it shouldn't be.
    >
    > As I said: XSLT is a programming language, so there are
    > generally multiple reasonable solutions to any "how can I"
    > question.


    Joe Kesselman <> wrote in
    <>:
    > (... Yes, I know, the "avoid for-each" philosophy. I'm not
    > dogmatic about that.)


    No, that wasn't my point in this case, actually. :) It's
    just that your explanation sounded (to me, at least) as if
    position() takes ordering defined by xsl:sort into the
    account only in context of xsl:for-each, so I wanted to
    clarify that this applies whenever ordered current node
    lists are involved.

    --
    It is rare to find learned men who are clean, do not stink,
    and have a sense of humour. -- Liselotte in a letter to
    Sophie, 30 Jul 1705
     
    Pavel Lepin, Oct 3, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Searle
    Replies:
    2
    Views:
    9,090
    Alan Searle
    Nov 20, 2005
  2. Sjaakie
    Replies:
    10
    Views:
    764
    Nick Gilbert
    Sep 21, 2006
  3. Harry Haller
    Replies:
    3
    Views:
    1,246
    RobinS
    Feb 1, 2007
  4. Plateriot

    multi-filtering and sorting a gridview

    Plateriot, Dec 15, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    988
    Plateriot
    Dec 15, 2008
  5. Michelle
    Replies:
    1
    Views:
    253
    Elton Wang
    Apr 8, 2005
Loading...

Share This Page