Subject: Joining Nodes (HELP!!)
Posted By: MissHenesy Post Date: 10/12/2006 4:26:03 PM
Dear People,

Does anyone know how to use XSLT / XPATH to join 2 different sets of nodes and produce results??  

For example, below is a source XML file.  What I would like to do is:
--------------------------------
Spit out the FOOD_IDs where the associated FOOD_TYPE_ID is equal to "1".
--------------------------------

The format of the xml file below mimics the actual structure of the XML document I have at work (which doesn't deal with anything nearly as interesting as waffles and french toast - wish it did!).

I've been researching XSL and XPATH for a few days now and still have not managed to find a way to do this seemingly simple task.

So - since I'm at wit's end - I'm begging for help!  

HELP!

Thank you,
Susan :o)

  








<breakfast_menu>
<!--***** FOOD *******-->
<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>French Toast</name>
    <price>$4.50</price>
    <description>thick slices made from our homemade sourdough bread</description>
    <calories>600</calories>
</food>

<!--****** FOOD TYPES *******-->
<!-- Food Type 1: WAFFLES -->
<food_type>    
    <food_type_id>1</food_type_id>
    <food_id>1</food_id>
</food_type>

<food_type>
    <food_type_id>1</food_type_id>
    <food_id>2</food_id>
</food_type>

<food_type>    
    <food_type_id>1</food_type_id>
    <food_id>3</food_id>
</food_type>

<!-- Food Type 2: FRENCH TOAST -->
<food_type>
    <food_type_id>2</food_type_id>
    <food_id>3</food_id>
</food_type>
 
</breakfast_menu>

Susan :)
Reply By: mhkay Reply Date: 10/12/2006 5:33:36 PM
>Spit out the FOOD_IDs where the associated FOOD_TYPE_ID is equal to "1".

In your data a food ID can have more than one associated food type ID.

You don't actually need a join for this, you can write

//food_type[food_type_id=1]/food_id

That will give you duplicates - there is a whole literature on how to eliminate duplicates (search for "XSLT grouping") (it's easy in XPath 2.0 using distinct-values()).

A better use case for a join is to list the food names with food type id = 1, that is

//food[id = //food_type[food_type_id=1]/food_id]/name

But in XSLT you would normally use keys to do this, for readability and for performance.

 



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: MissHenesy Reply Date: 10/17/2006 5:26:34 PM
Dear Michael Kay,

Thank you!!  I was very close -- turns out I was off by one "/".

I'm glad you responded, because you set me off looking for info about keys and getting distinct values, and I've since learned an arsenal about XSLT/XPATH in the past couple of days.  I've refined my original files and hope to get better at this.

Eventually :o).

Much gratitude,
Susan


Go to topic 51132

Return to index page 146
Return to index page 145
Return to index page 144
Return to index page 143
Return to index page 142
Return to index page 141
Return to index page 140
Return to index page 139
Return to index page 138
Return to index page 137