can I simplify this?

P

Patrick

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
 
P

Peyo

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

Joseph Kesselman

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?"
 
D

delirio

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>
 
P

Patrick

Joseph said:
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
 
P

Patrick

delirio said:
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
 
P

Patrick

Peyo said:
Patrick a écrit :



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
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top