473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

for-each help

Hello All,

I am new to xml and trying to make it work. I'm having a bit of trouble
with for-each loops.

I have this xml document;
<http://ocgserv.marine.usf.edu/patrick/xml/test_xml_copy.xml>

and this stylesheet:
<http://ocgserv.marine.usf.edu/patrick/xml/test_style_copy.xsl>

and would like it to look like this;
<http://ocgserv.marine.usf.edu/patrick/xml/example.html>

I can get it to read and show serial number and the green bar for each
weatherpak node or I can get it to show all of the info for one
weatherpak node, but I cant read from top to bottom and have it output
like I want. I've played around with moving the for-eachs. Can someone
tell me how to make this work correctly, or tell me there is no way it
can be done the way I want it.

Thanks,
Patrick
--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group – USF - College of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld
Apr 12 '06 #1
4 1504
On Wed, 12 Apr 2006 14:49:17 -0400, Patrick <ps****@marine.usf.edu>
wrote:
I am new to xml and trying to make it work. I'm having a bit of trouble
with for-each loops.


This is either easy, or hard (but well-known).

Sorry, but I'm at home now and don't have an editor to hand to write the
full code. But try this

<xsl:for-each select="met_packages/weatherpak">
<tr> stuff </tr>

<xsl:for-each select="./sensors">
<tr> <xsl:value-of select="./sensor"/> More stuff </tr>
</xsl:for-each>
</xsl:for-each>

The trick is to _nest_ the for-each loops and to use the context node
"." as your starting point for the inner one.
If this doesn't work (if you have multiple weatherpak elements for the
same ID) then it becomes a grouping problem more than a nesting problem.
It's compelx and puzzling to see for the first time, but there's a
well-known solution to this called "Muenchian Grouping". Web searchign
for this (probably on Jeni Tennison's site) should sort you out.
--
Cats have nine lives, which is why they rarely post to Usenet.
Apr 12 '06 #2
Andy Dingley wrote:
On Wed, 12 Apr 2006 14:49:17 -0400, Patrick <ps****@marine.usf.edu>
wrote:

I am new to xml and trying to make it work. I'm having a bit of trouble
with for-each loops.

This is either easy, or hard (but well-known).

Sorry, but I'm at home now and don't have an editor to hand to write the
full code. But try this

<xsl:for-each select="met_packages/weatherpak">
<tr> stuff </tr>

<xsl:for-each select="./sensors">
<tr> <xsl:value-of select="./sensor"/> More stuff </tr>
</xsl:for-each>
</xsl:for-each>

The trick is to _nest_ the for-each loops and to use the context node
"." as your starting point for the inner one.
If this doesn't work (if you have multiple weatherpak elements for the
same ID) then it becomes a grouping problem more than a nesting problem.
It's compelx and puzzling to see for the first time, but there's a
well-known solution to this called "Muenchian Grouping". Web searchign
for this (probably on Jeni Tennison's site) should sort you out.

Thanks Andy, I will take at look at this.

Patrick

--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group – USF - College of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld

Apr 13 '06 #3
> I can get it to read and show serial number and the green bar for each
weatherpak node or I can get it to show all of the info for one
weatherpak node, but I cant read from top to bottom and have it output
like I want.


I'm not sure I understand what you mean by "read from top to bottom".

(I'm also not sure why you expect to see green when your stylesheet
explicitly says to use greys.)
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 14 '06 #4
Patrick schrieb:
Hello All,

I am new to xml and trying to make it work. I'm having a bit of trouble
with for-each loops.

Hi Patrick,

although your problem isn't quit clear. The nested for-each loops are
always a hazard. Simply dont use them ....

use templates on your tree. A bit more wordy - but a lot more flexible
and editable

message continued after the code
----------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output omit-xml-declaration="yes" method="xml" />
<xsl:template match="/">
<!-- stuff for the html output -->
<xsl:element name="html">
<xsl:element name="head">
<xsl:element name="title">
xsl w/o for-each
</xsl:element>
</xsl:element>

<xsl:element name="body">
<xsl:apply-templates />
</xsl:element>
</xsl:element>
</xsl:template>

<xsl:template match="met_packages">
<!-- the met packages make a table -->
<xsl:element name="table">
<xsl:apply-templates />
</xsl:element>
</xsl:template>

<xsl:template match="weatherpak">
<!-- first theHeader -->
<xsl:element name="tr">
<xsl:element name="th">
<xsl:attribute name="align">left</xsl:attribute>
WeatherPak
</xsl:element>
<xsl:element name="th">
<xsl:attribute name="colspan">4</xsl:attribute>
<xsl:value-of select="serial_number" />
</xsl:element>
<xsl:element name="tr">
<xsl:attribute name="bgcolor">#9acd32</xsl:attribute>
<th align="left">Sensor</th>
<th align="left">Sensor Type</th>
<th align="left">Range</th>
<th align="left">Accuracy</th>
<th align="left">Resolution</th>
</xsl:element>
<xsl:apply-templates select="sensors"/>
</xsl:element>
</xsl:template>
<xsl:template match="sensors">
<xsl:element name="tr">
<xsl:apply-templates />
</xsl:element>
</xsl:template>

<xsl:template match="sensors/*">
<xsl:element name="td">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>

<xsl:template match="*|@*" />
</xsl:stylesheet>

----------------------------------------

this is a bit mor flexible eg. you want to highlight the resolution just
add:

<xsl:template match="sensors/resolution">
<xsl:element name="td">
<b><xsl:value-of select="." /></b>
</xsl:element>
</xsl:template>

HIH

Jo
just add:
<xsl:template match
Apr 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xerxes | last post by:
Hi, is there any script to authenticate an email address entered in a form field? I used the php mail() function, using the following (where my email field on the form is called "email"): ...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
0
by: Verizon | last post by:
Has anybody ever heard of support for the book: "Secure PHP Development" by: Mohammed J. Kabir I'm trying to run one of his PHP solutions called "Web Forms Manager" I haven't been able to...
0
by: panic | last post by:
If you are not familiar with the palm DB apps I am refering to, they are applications that allow you to graphically create a db by adding fields(columns) and then creats a form so you can enter the...
0
by: Robert Freund | last post by:
I've been looking for a good and free code beautifier that runs on linux. About the only ones I found where Trita (www.trita.com), which costs money and only runs on windows. The other one was...
33
by: Frank | last post by:
What is the best IDE for developing PHP applications? What do you use and why? Thanks.
4
by: john Q | last post by:
I found a PHP based email obfuscator for use in websites, where the source HTML (PHP, actually) file doesn't contain the actual email address in the Mailto tag, but generates it and that...
0
by: Mr.Bogi | last post by:
Is anyone aware of an open source report generation library for php/mysql? Basically a freeware/opensource alternative to Crystal Reports. thanks
0
by: Alex | last post by:
Bottom line: would like to get a weblog and bulletin board going. Would like to use phpBB2 and movabletype. I have movabletype working using mysql. Apache and php were on my linux red hat 7.2...
5
by: ChronicFatigue | last post by:
Hello My current host has register_globals switched on in their php.ini file. Would it be prudent for me to design code which works when register_globals is switched off in case I switch hosts...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.