473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

distinct grouping of sorted results

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

Jul 20 '05 #1
4 3537
Tempore 05:35:01, die Wednesday 09 February 2005 AD, hinc in foro {comp.text.xml} scripsit <kr********@gmail.com>:
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:output 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-v...i?userid=38041)
Fiat W3C in tenebris
Jul 20 '05 #2
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...

Jul 20 '05 #3
Tempore 11:20:03, die Wednesday 09 February 2005 AD, hinc in foro {comp.text.xml} scripsit <kr********@gmail.com>:
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:output 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-v...i?userid=38041)
Fiat W3C in tenebris
Jul 20 '05 #4
That did it. Thanks a million!

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
5
by: Mike King | last post by:
I don't know how to group the following data in the way I want it. I want the output of the transformation to be "5678". Does anyone know what I am doing worry? <?xml version="1.0"?> <data>...
5
by: Chris Kettenbach | last post by:
Good Morning, Sorry for xposting. Just need a liitle help. I have an xml file that's generated from a database. How do I select distinct values from a field in xslt and then loop through the...
2
by: Taryon | last post by:
Hi all! i have an xml dataset with places and monuments. several monuments for one place than the place appears several times. how can i can read the dataset like DISTINCT ROWS? or how can i make...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
3
by: Yogs | last post by:
Hello All, I have an RSS XML file that is read in and then displayed on the site. The RSS file has repeating news articles, is there a way to tell the XmlDocument object to display the article...
1
by: Patrick.O.Ige | last post by:
I have a xml file and i want to format it using XSL My XSL file and XML below I needed to do a distinct which is ok on the first node "Code" For the "programDescription" i did below which gets the...
1
by: Chris | last post by:
I have a problem with grouping elements in my XSL. What I want to do is select all of the distinct SCE_CGPC records, then by the FSG_SNAM records for each SCE_CGPC and then again by MOA_NAME for...
1
by: mianiro | last post by:
I want to use a distinct and a top in a select statement, however I want the distinct to only apply to one of the columns. For example SELECT DISTINCT TOP 15 col1,col2,col3,col4 from table1 I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.