Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old June 8th, 2007, 08:35 PM
Patrick
Guest
 
Posts: n/a
Default 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


  #2  
Old June 8th, 2007, 08:45 PM
Peyo
Guest
 
Posts: n/a
Default Re: can I simplify this?

Patrick a écrit :
Quote:
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?
Quote:
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.
  #3  
Old June 8th, 2007, 09:25 PM
Joseph Kesselman
Guest
 
Posts: n/a
Default Re: can I simplify this?

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
  #4  
Old June 8th, 2007, 11:15 PM
delirio
Guest
 
Posts: n/a
Default Re: can I simplify this?

On Jun 8, 8:24 pm, Patrick <psm...@marine.usf.eduwrote:
Quote:
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>

  #5  
Old June 11th, 2007, 02:35 PM
Patrick
Guest
 
Posts: n/a
Default Re: can I simplify this?

Joseph Kesselman wrote:
Quote:
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

  #6  
Old June 11th, 2007, 02:35 PM
Patrick
Guest
 
Posts: n/a
Default Re: can I simplify this?

delirio wrote:
Quote:
On Jun 8, 8:24 pm, Patrick <psm...@marine.usf.eduwrote:
>
Quote:
>>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

  #7  
Old June 11th, 2007, 02:35 PM
Patrick
Guest
 
Posts: n/a
Default Re: can I simplify this?

Peyo wrote:
Quote:
Patrick a écrit :
>
Quote:
>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?
>
>
Quote:
>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

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles