Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 12th, 2006, 04:26 PM
Authorized User
 
Join Date: Jun 2003
Location: New York, NY, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Joining Nodes (HELP!!)

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_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_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>
    <food_type_id>2</food_type_id>
    <food_id>3</food_id>
</food_type>

</breakfast_menu>

Susan :)
__________________
Susan :)
 
Old October 12th, 2006, 05:33 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>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
 
Old October 17th, 2006, 05:26 PM
Authorized User
 
Join Date: Jun 2003
Location: New York, NY, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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




Similar Threads
Thread Thread Starter Forum Replies Last Post
Joining Three Tables Alexpizzoferro Classic ASP Databases 5 September 1st, 2006 04:08 PM
Joining 2 variables together for one name Lucy Classic ASP Basics 1 June 13th, 2006 10:01 AM
Joining 3 Queries. rupen SQL Language 1 May 11th, 2006 11:35 AM
joining 3 tables koskalgr MySQL 5 March 24th, 2006 09:52 AM
Joining two tables rajustha SQL Server 2000 1 December 7th, 2003 07:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.