XSL sorting of results from recursive calls to document()?

Discussion in 'XML' started by felciano, Oct 6, 2008.

  1. felciano

    felciano Guest

    Hello --

    I am trying to use XSL to process Amazon wishlist data to sort the
    results by type (Apparel, then Books, then DVDs, etc). Amazon's web
    services chunk up results in multiple pages of fixed size, e.g. 55
    items gets returned in 5 XML pages of 10 items and a 6th of 5 items.
    Each page is returned from a distinct URL call with a PageNum
    parameter.

    I've been trying to adapt a technique described at http://www.xefer.com/amazon/wishlist
    that shows how to iterate through such pages through recursive
    document() calls and a document counter:

    <xsl:template name="counter">
    <xsl:param name="total"/>
    <xsl:param name="page"/>

    <xsl:variable name="lookup"
    select="concat($lookup, '&amp;ProductPage=', $page)

    <xsl:if test="$page <= $total">
    <xsl:apply-templates select="document($lookup)"/>
    <xsl:call-template name="counter">
    <xsl:with-param name="total" select="$total"/>
    <xsl:with-param name="page" select="$page + 1"/>
    </xsl:call-template>
    </xsl:if>

    </xsl:template>

    This works, but I can't figure out how to sort the aggregate results.
    Each document's nodes are processed separately before the next page is
    retrieved. Is there a convention / idiom for how to handle sorting
    when iterating via recursion across multiple document() calls?

    Thanks,

    Ramon
     
    felciano, Oct 6, 2008
    #1
    1. Advertising

  2. felciano wrote:

    > I've been trying to adapt a technique described at http://www.xefer.com/amazon/wishlist
    > that shows how to iterate through such pages through recursive
    > document() calls and a document counter:
    >
    > <xsl:template name="counter">
    > <xsl:param name="total"/>
    > <xsl:param name="page"/>
    >
    > <xsl:variable name="lookup"
    > select="concat($lookup, '&amp;ProductPage=', $page)
    >
    > <xsl:if test="$page <= $total">
    > <xsl:apply-templates select="document($lookup)"/>
    > <xsl:call-template name="counter">
    > <xsl:with-param name="total" select="$total"/>
    > <xsl:with-param name="page" select="$page + 1"/>
    > </xsl:call-template>
    > </xsl:if>
    >
    > </xsl:template>
    >
    > This works, but I can't figure out how to sort the aggregate results.
    > Each document's nodes are processed separately before the next page is
    > retrieved. Is there a convention / idiom for how to handle sorting
    > when iterating via recursion across multiple document() calls?


    Instead of using xsl:apply-templates select="document($lookup)" during
    each template invocation you need to pass on those results the document
    call returns. So give your template a third parameter

    <xsl:template name="counter">
    <xsl:param name="total"/>
    <xsl:param name="page"/>
    <xsl:param name="page-elements" select="/.."/>

    <xsl:variable name="lookup"
    select="concat($lookup, '&amp;ProductPage=', $page)

    <xsl:choose>
    <xsl:when test="$page <= $total">
    <xsl:call-template name="counter">
    <xsl:with-param name="total" select="$total"/>
    <xsl:with-param name="page" select="$page + 1"/>
    <xsl:with-param name="page-elements" select="$page-elements |
    document($lookup)/*"/>
    </xsl:call-template>
    </xsl:choose>
    <xsl:eek:therwise>
    <xsl:apply-templates select="$page-elements">
    <xsl:sort select="foo"/>
    </xsl:apply-templates/>
    </xsl:eek:therwise>
    </xsl:choose

    </xsl:template>




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 6, 2008
    #2
    1. Advertising

  3. felciano

    felciano Guest

    Very cool -- thank you for the quick response!

    Ramon
     
    felciano, Oct 10, 2008
    #3
    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. Monique Y. Mudama
    Replies:
    1
    Views:
    458
    Monique Y. Mudama
    Jun 28, 2005
  2. Sparko
    Replies:
    0
    Views:
    411
    Sparko
    Jul 9, 2004
  3. Replies:
    1
    Views:
    3,623
    A. Bolmarcich
    May 27, 2005
  4. hagar
    Replies:
    1
    Views:
    384
    Simon Brooke
    Oct 19, 2006
  5. n00m
    Replies:
    12
    Views:
    1,119
Loading...

Share This Page