xsl stylesheet problem selecting specific nodes

Discussion in 'XML' started by doulos05@gmail.com, Apr 5, 2007.

  1. Guest

    Ok, I'm sure the answer is right in front of me, but I can't see if
    for the life of me. Here's my xml document:

    <price_guide>
    <group><name>Wacky Widgets</name>
    <product>
    <product_id>1</product_id>
    <description>Widget 1</description>
    <sell>
    <quantity>500</quantity>
    <price>25.50</qunatity>
    </sell>
    <sell>
    <quantity>1000</quantity>
    <price>34.20</price>
    </sell>
    </product>
    <!-- Multiple producst here-->
    </group>
    <group><name>Cool Computers</name>
    <product>
    <product_id>2</product_id>
    <description>Computer 1</description>
    <sell>
    <quantity>500</quantity>
    <price>50.43</price>
    </sell>
    <sell>
    <quantity>1000</quantity>
    <price>83.10</price>
    </sell>
    <sell>
    <quantity>2500</quantity>
    <price>160.76</price>
    </sell>
    </product>
    </group>
    </price_guide>

    Now, I want a stylesheet that takes each group and puts it in a
    separate HTML table with the Name as an <h2> header and Product ID,
    Description, and each quantity as a Column header. Apparently there
    are too many "for-each" loops in this for my small brain to handle.
    What I'm getting is that is makes a table for each group containing
    every product in every group, rather than every product in that
    particular group. What do I need to do this without having to say /
    product_guide/group[n]/product every time?
     
    , Apr 5, 2007
    #1
    1. Advertising

  2. wrote:
    > Now, I want a stylesheet that takes each group and puts it in a
    > separate HTML table with the Name as an <h2> header and Product ID,
    > Description, and each quantity as a Column header. Apparently there
    > are too many "for-each" loops in this for my small brain to handle.


    For-each loops should be a last resort. It's almost always simpler, and
    easier to maintain, if you just code templates and apply-template
    requests, and let XSLT do the looping.

    Root template: match "/", and apply-templates with select="group"

    Group template: match "group", and do the processing for a group: get
    the name and render it as an h2, start the table and apply-templates
    against "product"; then end the table.

    Product template: match "product", start a tr, process the contents into
    tds by further apply-templates, end the tr.

    In other words, let XSLT keep track of context, while you focus on
    processing one node at a time.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Apr 5, 2007
    #2
    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. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    437
    Patrick TJ McPhee
    Jun 17, 2004
  2. Thomas Sommer
    Replies:
    0
    Views:
    446
    Thomas Sommer
    Nov 25, 2004
  3. MC
    Replies:
    1
    Views:
    7,363
    David Carlisle
    Mar 24, 2005
  4. Replies:
    0
    Views:
    560
  5. Replies:
    1
    Views:
    620
    Soren Kuula
    Aug 24, 2006
Loading...

Share This Page