xsl stylesheet problem selecting specific nodes

D

doulos05

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?
 
J

Joseph Kesselman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top