Get distinct values on *second* grouping of nodes.
I'm stumped again!!!
I have stumbled upon a task that I am having trouble solving.
I have the following XML file (see below) which contains information about breakfast foods.
For my first task, I have successfully grouped a list of FOOD under their related FOOD_TYPES. (Many thank yous to Michael Kay for setting me on the right path with that one.)
Now for my second trick, I want to show that I can choose two or more FOOD TYPES, and list a distinct group of FOOD under those types.
This has got to be simple, but it is proving to be beyond me.
Immediately below is my XML file - and below that, XSL code that I am working on. My version of MSXML does not seem to understand the keyword "distinct-value", so I can't use that. I thought that if I used a "for-each" loop for each FOOD TYPE and then nested another "for-each" loop within that one -- which would use KEYS and the COUNT[] method to return only unique FOODS -- that that would work, but it doesn't.
I tried to use the "generate-id" idea but that didn't work, either.
The best I can guess is that since my first "for-each" loop is looping through each FOOD_ID found in FOOD_TYPE (which has the duplicate info...generate_id didn't work ... because it just created a unique id for something that repeated.
As for the count[] method not working in the nested "for-each" loop ... I am stumped ... I must not understand how those nodes find each other.
Basically, if I choose FOOD_TYPE where the food_type_id = 1 or food_type_id = 4, I do *NOT* want to see "Belgian Waffles" repeat itself just because it is in those two groups.
So perhaps I might need to revert to a "preceding-sibling" maneuver, but I'll admit I tried that earlier, and THAT didn't work.
Which is why I am hollering for help. HELP!!!!!
Apologies for this rudimentary post,
Susan
---------------------------------------
FOOD.XML
---------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<breakfast_menu>
<food>
<food_id>1</food_id>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<food_id>2</food_id>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<food_id>3</food_id>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<food_id>4</food_id>
<name>French Toast</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<food_id>5</food_id>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
<food_type>
<food_type_id>1</food_type_id>
<food_type_name>WAFFLES</food_type_name>
<food_id>1</food_id>
</food_type>
<food_type>
<food_type_id>1</food_type_id>
<food_type_name>WAFFLES</food_type_name>
<food_id>2</food_id>
</food_type>
<food_type>
<food_type_id>1</food_type_id>
<food_type_name>WAFFLES</food_type_name>
<food_id>3</food_id>
</food_type>
<food_type>
<food_type_id>2</food_type_id>
<food_type_name>FRENCH TOAST</food_type_name>
<food_id>4</food_id>
</food_type>
<food_type>
<food_type_id>3</food_type_id>
<food_type_name>TRADITIONAL BREAKFASTS</food_type_name>
<food_id>5</food_id>
</food_type>
<food_type>
<food_type_id>4</food_type_id>
<food_type_name>COMBO BREAKFASTS</food_type_name>
<food_id>5</food_id>
</food_type>
<food_type>
<food_type_id>4</food_type_id>
<food_type_name>COMBO BREAKFASTS</food_type_name>
<food_id>1</food_id>
</food_type>
</breakfast_menu>
---------------------------------------
FOOD.XSL
---------------------------------------
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:key name="food" match="food" use="food_id"/>
<xsl:key name="foodType" match="food_type" use="food_type_id"/>
<xsl:template match="/">
<html>
<body>
<p>
<h3>Example 1</h3>
<b>Display a list of foods, grouped by food type</b>
<xsl:for-each select="//food_type[count(. | key('foodType',food_type_id)[1]) = 1]">
<p>
<xsl:variable name="foodType" select="food_type_id"/>
<xsl:value-of select="food_type_id"/>)
<xsl:text> </xsl:text>
<xsl:value-of select="food_type_name"/>
<br></br>
<xsl:for-each select="//food_type[food_type_id = $foodType]/food_id">
<xsl:variable name="foodID" select="."/>
Breakfast Name: <i><xsl:value-of select="//food[food_id=$foodID]/name"/>
</i><br></br>
</xsl:for-each>
</p>
</xsl:for-each>
</p>
<p>
<h3>Example 2</h3>
<p>
<b>Show a unique list of Breakfast Items that belong to food types "Waffles" and "Combos".</b>
</p>
<xsl:for-each select="//food_type[count(. | key('foodType',food_type_id)[1]) = 1]">
<xsl:variable name="foodType" select="food_type_id"/>
<xsl:if test="$foodType = 1 or $foodType=4">
[u]<xsl:value-of select="food_type_id"/>)<xsl:text> </xsl:text><xsl:value-of select="food_type_name"/>
</u>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
<br></br>
<!--||************************************************ **************
|| TROUBLE AREA - CAN'T GET DISTINCT FOOD IDS!!!!!
||************************************************ **************-->
<xsl:for-each select="//food_type[food_type_id=1 or food_type_id=4]">
<xsl:sort select="food_id"/>
<xsl:variable name="foodType" select="food_type_id"/>
<xsl:variable name="foodID" select="food_id"/>
<xsl:for-each select="//food[count(. | key('food', $foodID)[1])=1]">
<li><xsl:value-of select="name"/></li>
</xsl:for-each>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
__________________
Susan :)
|