distinct grouping of sorted results

Discussion in 'XML' started by kristofera@gmail.com, Feb 9, 2005.

  1. Guest

    I am trying to do a distinct grouping of some nodes sorted by a numeric
    value but for some reason the distinct (preceding-sibling filter) is
    applied to the result as if not sorted. If I don't use the
    preceding-sibling filter, the nodes are properly sorted. Is this a bug
    in the xslt processor I'm using (.net framework 1.1) or is this correct
    behaviour?

    Any alternative solutions that will show me the lowest priced item in
    each category? (xml and xslt snippets below)

    XML:

    <Items>
    <Item>
    <ItemCategory>Cat A</ItemCategory>
    <ItemName>Item 1</ItemName>
    <Price>5</Price>
    </Item>
    <Item>
    <ItemCategory>Cat B</ItemCategory>
    <ItemName>Item 2</ItemName>
    <Price>3</Price>
    </Item>
    <Item>
    <ItemCategory>Cat A</ItemCategory>
    <ItemName>Item 3</ItemName>
    <Price>2</Price>
    </Item>
    </Items>



    XSLT snippets:

    <xsl:apply-templates select="Item">
    <xsl:sort select="ItemCategory" />
    <xsl:sort select="Price" data-type="number" order="ascending" />
    </xsl:apply-templates>

    <xsl:template match="Item">
    <xsl:if test="not(ItemCategory=preceding-sibling::Item/ItemCategory)">
    Item: <xsl:value-of select="ItemName" />
    Price: <xsl:value-of select="Price" />
    </xsl:if>
    </xsl:template>


    Expected output:
    Item: Item 3
    Price: 2

    Item: Item 2
    Price: 3


    Actual output:
    Item: Item 1
    Price: 5

    Item: Item 2
    Price: 3
     
    , Feb 9, 2005
    #1
    1. Advertising

  2. Joris Gillis Guest

    Tempore 05:35:01, die Wednesday 09 February 2005 AD, hinc in foro {comp.text.xml} scripsit <>:

    > I am trying to do a distinct grouping of some nodes sorted by a numeric
    > value but for some reason the distinct (preceding-sibling filter) is
    > applied to the result as if not sorted. If I don't use the
    > preceding-sibling filter, the nodes are properly sorted. Is this a bug
    > in the xslt processor I'm using (.net framework 1.1) or is this correct
    > behaviour?


    Hi,

    This is correct behaviour, the 'preceding-sibling' axis contains nodes in (reversed) document order, while the order of the nodes in the current node-set within your template 'Item' is altered by the sorting instruction. This order conflict results in unexpected behaviour.

    Try it with the muenchian grouping:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:eek:utput method="xml" indent="yes"/>
    <xsl:key name="cat" match="Item" use="ItemCategory"/>

    <xsl:template match="Items">
    <xsl:for-each select="Item[generate-id()=generate-id(key('cat',ItemCategory))]">
    <xsl:sort select="ItemCategory" />
    <xsl:apply-templates select="key('cat',ItemCategory)">
    <xsl:sort select="Price" data-type="number" order="ascending" />
    </xsl:apply-templates>
    </xsl:for-each>
    </xsl:template>

    <xsl:template match="Item">
    <xsl:if test="position()=1">
    Item: <xsl:value-of select="ItemName" />
    Price: <xsl:value-of select="Price" />
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>




    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Fiat W3C in tenebris
     
    Joris Gillis, Feb 9, 2005
    #2
    1. Advertising

  3. Guest

    Thanks.

    I tried the Muenchian grouping but couldn't get it to work. I'm not
    sure I fully understand how the Muenchian grouping works but suspect
    the reason it doesn't work for me is that I have several subtrees
    similar to the Items/Item tree in my document:

    <Stores>
    <Store>
    <Items>
    <Item>
    .....
    <Store>
    <Items>
    <Item>
    .....

    ....and a subsequent key lookup will go off and grab stuff from the
    wrong subtree...
     
    , Feb 9, 2005
    #3
  4. Joris Gillis Guest

    Tempore 11:20:03, die Wednesday 09 February 2005 AD, hinc in foro {comp.text.xml} scripsit <>:

    > I tried the Muenchian grouping but couldn't get it to work. I'm not
    > sure I fully understand how the Muenchian grouping works but suspect
    > the reason it doesn't work for me is that I have several subtrees
    > similar to the Items/Item tree in my document:


    That ought to be the problem.

    Try this one:
    <xsl:stylesheet version="1.1"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" indent="yes"/>
    <xsl:key name="cat" match="Item" use="concat(generate-id(..),ItemCategory)"/>

    <xsl:template match="Items">
    <xsl:for-each select="Item[generate-id()=generate-id(key('cat',concat(generate-id(..),ItemCategory)))]">
    <xsl:sort select="ItemCategory" />
    <xsl:apply-templates select="key('cat',concat(generate-id(..),ItemCategory))">
    <xsl:sort select="Price" data-type="number" order="ascending" />
    </xsl:apply-templates>
    </xsl:for-each>
    </xsl:template>

    <xsl:template match="Item">
    <xsl:if test="position()=1">
    Item: <xsl:value-of select="ItemName" />
    Price: <xsl:value-of select="Price" />
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Fiat W3C in tenebris
     
    Joris Gillis, Feb 9, 2005
    #4
  5. Guest

    That did it. Thanks a million!
     
    , Feb 9, 2005
    #5
    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. Fredrik Rodin

    Grouping product list results in repeater

    Fredrik Rodin, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,878
    Craig Deelsnyder
    Jan 6, 2004
  2. Yogs

    XML Distinct

    Yogs, Feb 15, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,431
  3. Fernando Lopes
    Replies:
    2
    Views:
    593
    Peter Blum
    Nov 19, 2004
  4. Hicham Mouline
    Replies:
    1
    Views:
    415
    Kai-Uwe Bux
    Apr 11, 2010
  5. Max Williams

    Grouping results by similar names

    Max Williams, Apr 22, 2008, in forum: Ruby
    Replies:
    1
    Views:
    137
    Joachim (M√ľnchen)
    Apr 23, 2008
Loading...

Share This Page