473,325 Members | 2,792 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,325 software developers and data experts.

can I simplify this?

Hi All,

Kind of new to this. What I have below works, but I am wondering if
there is a way to make it more efficient/simpler instead of having to
write an if, tr, td, blah for each datatype. How would I simplify this?
Any thoughts are appreciated.

Thanks,
Patrick

Datatypes will be at leat 1 but could be up to 10 depending on the setup.

<datacollected>
<datatype1>ADCP Velocity Measurements</datatype1>
<datatype2>ADCP Temperature</datatype2>
<datatype3>ADCP Performance DataX</datatype3>
<datatype4>Seabird Microcat Data</datatype4>
<datatype5>WaDAR Temperature Data</datatype5>
<datatype6>Coastal Climate MET Data</datatype6>
</datacollected>

<xsl:for-each select="./datacollected"<th align="left"
bgcolor="#28609E">Data Collected</th>

<xsl:if test="datatype1">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype1"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype2">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype2"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype3">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype3"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype4">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype4"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype5">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype5"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype6">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype6"/></td><td align="center">X</td>
</tr>
</xsl:if>
</xsl:for-each>
--
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
Jun 8 '07 #1
6 1866
Patrick a écrit :
Kind of new to this. What I have below works, but I am wondering if
there is a way to make it more efficient/simpler instead of having to
write an if, tr, td, blah for each datatype. How would I simplify this?
Datatypes will be at leat 1 but could be up to 10 depending on the setup.

<datacollected>
<datatype1>ADCP Velocity Measurements</datatype1>
<datatype2>ADCP Temperature</datatype2>
<datatype3>ADCP Performance DataX</datatype3>
<datatype4>Seabird Microcat Data</datatype4>
<datatype5>WaDAR Temperature Data</datatype5>
<datatype6>Coastal Climate MET Data</datatype6>
</datacollected>

<xsl:if test="datatype1">
<xsl:if test="datatype2">
<xsl:if test="datatype3">
<xsl:if test="datatype4">
<xsl:if test="datatype5">
<xsl:if test="datatype6">
The *local name* of your elements is "datatype1", "datatype2",
"datatype3"... right ? So what about testing *any* element *whose* local
name *starts with* "datatype" ? ;-)

Cheers,

p.
Jun 8 '07 #2
Obvious simplification: Factor out the boilerplate -- use templates
rather than conditionals.

<xsl:for-each select="./datacollected">
<th align="left" bgcolor="#28609E">Data Collected</th>
<xsl:apply-templates>
</xsl:for-each>

and add

<xsl:template match="datatype1 | datatype2 | datatype3 | datatype4 |
datatype5 | datatype6">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="."/></td><td align="center">X</td>
</tr>
</xsl:template>

This does assume the datatypeWhatever elements will appear in the
desired order; if not, you can fix that by doing six more-selective
applyTemplates calls.

Actually, you might want to change whatever is calling the for-each to
also take advantage of apply-templates.

It is usually (but not always) better to let XSLT do the looping and
matching for you, and keep your own focus on "what do I want to do with
it once I've found it?"

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Jun 8 '07 #3
On Jun 8, 8:24 pm, Patrick <psm...@marine.usf.eduwrote:
Hi All,

Kind of new to this. What I have below works, but I am wondering if
there is a way to make it more efficient/simpler instead of having to
write an if, tr, td, blah for each datatype. How would I simplify this?
Any thoughts are appreciated.

Thanks,
Patrick

Datatypes will be at leat 1 but could be up to 10 depending on the setup.

<datacollected>
<datatype1>ADCP Velocity Measurements</datatype1>
<datatype2>ADCP Temperature</datatype2>
<datatype3>ADCP Performance DataX</datatype3>
<datatype4>Seabird Microcat Data</datatype4>
<datatype5>WaDAR Temperature Data</datatype5>
<datatype6>Coastal Climate MET Data</datatype6>
</datacollected>

<xsl:for-each select="./datacollected"<th align="left"
bgcolor="#28609E">Data Collected</th>

<xsl:if test="datatype1">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype1"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype2">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype2"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype3">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype3"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype4">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype4"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype5">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype5"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype6">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype6"/></td><td align="center">X</td>
</tr>
</xsl:if>
</xsl:for-each>
--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group - USF - College of Marine Sciencehttp://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
<xsl:template match="/datacollected">
<table>
<th align="left" bgcolor="#28609E">Data Type</th>
<th align="left" bgcolor="#28609E">Data Collected</th>
<xsl:apply-templates select="*[contains(name(),
'datatype')]"/>
</table>
</xsl:template>

<xsl:template match="*[contains(name(), 'datatype')]">
<tr>
<td class="dt" bgcolor="#92ADC8">
<xsl:value-of select="."/>
</td>
<td align="center">X</td>
</tr>
</xsl:template>

Jun 8 '07 #4
Joseph Kesselman wrote:
Obvious simplification: Factor out the boilerplate -- use templates
rather than conditionals.

<xsl:for-each select="./datacollected">
<th align="left" bgcolor="#28609E">Data Collected</th>
<xsl:apply-templates>
</xsl:for-each>

and add

<xsl:template match="datatype1 | datatype2 | datatype3 | datatype4 |
datatype5 | datatype6">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="."/></td><td align="center">X</td>
</tr>
</xsl:template>

This does assume the datatypeWhatever elements will appear in the
desired order; if not, you can fix that by doing six more-selective
applyTemplates calls.

Actually, you might want to change whatever is calling the for-each to
also take advantage of apply-templates.

It is usually (but not always) better to let XSLT do the looping and
matching for you, and keep your own focus on "what do I want to do with
it once I've found it?"
Thanks for your reply. I will take a look at this to see how it works. I
had read something, somewhere about template matching but it was kind of
confusing to me. This gives me a bit of an example to go on.

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

Jun 11 '07 #5
delirio wrote:
On Jun 8, 8:24 pm, Patrick <psm...@marine.usf.eduwrote:
>>Hi All,

Kind of new to this. What I have below works, but I am wondering if
there is a way to make it more efficient/simpler instead of having to
write an if, tr, td, blah for each datatype. How would I simplify this?
Any thoughts are appreciated.

Thanks,
Patrick

Datatypes will be at leat 1 but could be up to 10 depending on the setup.

<datacollected>
<datatype1>ADCP Velocity Measurements</datatype1>
<datatype2>ADCP Temperature</datatype2>
<datatype3>ADCP Performance DataX</datatype3>
<datatype4>Seabird Microcat Data</datatype4>
<datatype5>WaDAR Temperature Data</datatype5>
<datatype6>Coastal Climate MET Data</datatype6>
</datacollected>

<xsl:for-each select="./datacollected"<th align="left"
bgcolor="#28609E">Data Collected</th>

<xsl:if test="datatype1">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype1"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype2">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype2"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype3">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype3"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype4">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype4"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype5">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype5"/></td><td align="center">X</td>
</tr>
</xsl:if>
<xsl:if test="datatype6">
<tr>
<td class="dt" bgcolor="#92ADC8"><xsl:value-of
select="datatype6"/></td><td align="center">X</td>
</tr>
</xsl:if>
</xsl:for-each>
--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group - USF - College of Marine Sciencehttp://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


<xsl:template match="/datacollected">
<table>
<th align="left" bgcolor="#28609E">Data Type</th>
<th align="left" bgcolor="#28609E">Data Collected</th>
<xsl:apply-templates select="*[contains(name(),
'datatype')]"/>
</table>
</xsl:template>

<xsl:template match="*[contains(name(), 'datatype')]">
<tr>
<td class="dt" bgcolor="#92ADC8">
<xsl:value-of select="."/>
</td>
<td align="center">X</td>
</tr>
</xsl:template>
Thanks for your reply. I will take a look at this as it is very similar
to the response I received from Kesselman.

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

Jun 11 '07 #6
Peyo wrote:
Patrick a écrit :
>Kind of new to this. What I have below works, but I am wondering if
there is a way to make it more efficient/simpler instead of having to
write an if, tr, td, blah for each datatype. How would I simplify this?

>Datatypes will be at leat 1 but could be up to 10 depending on the setup.

<datacollected>
<datatype1>ADCP Velocity Measurements</datatype1>
<datatype2>ADCP Temperature</datatype2>
<datatype3>ADCP Performance DataX</datatype3>
<datatype4>Seabird Microcat Data</datatype4>
<datatype5>WaDAR Temperature Data</datatype5>
<datatype6>Coastal Climate MET Data</datatype6>
</datacollected>

<xsl:if test="datatype1">
<xsl:if test="datatype2">
<xsl:if test="datatype3">
<xsl:if test="datatype4">
<xsl:if test="datatype5">
<xsl:if test="datatype6">


The *local name* of your elements is "datatype1", "datatype2",
"datatype3"... right ? So what about testing *any* element *whose* local
name *starts with* "datatype" ? ;-)

Cheers,

p.
Thanks Peyo, I will take a 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

Jun 11 '07 #7

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

Similar topics

22
by: Alan | last post by:
I have many OR in the if statement, any method to simplify? if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......) // .... thank you
29
by: Flzw | last post by:
Alright, here is a simple function I coded, won't be hard understanding what it does, and YES, I know, you will probably tell me it's awful code, I would like to know how to write it better, maybe...
0
by: Stylus Studio | last post by:
Stylus Studio 6 XML Enterprise Edition Now Integrates with TigerLogic XDMS XQuery and Native XML Database Bedford, MA, -- Stylus Studio ( http://www.stylusstudio.com ), the industry-leading...
0
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
3
by: Bob Bedford | last post by:
hello I'm looking for some functions or objects allowing to select-insert-update-delete from any table in a mysql database without the need to create a new query every time. Example: ...
6
by: jsceballos | last post by:
Hello. I'm writing a proxy class, i.e: a class whose methods mostly delegate their functionality to other class object. Most of the methods (which are quite a lot) defined in the class would end...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
1
AmLegacy
by: AmLegacy | last post by:
I'm having a hard time figuring out how to simplify the fractions. Can anyone look at this code and see if you can see something I don't. //This is the fraction adding function void add_fractions...
3
by: Hvid Hat | last post by:
Hi I've got the following XSLT that works but I'm trying to figure out how to simplify it. There is only one Image, so I don't need the for-each-loop. Anyone? <xsl:for-each select="Data/img">...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.