Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > can I simplify this?

Reply
Thread Tools

can I simplify this?

 
 
Patrick
Guest
Posts: n/a
 
      06-08-2007
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


 
Reply With Quote
 
 
 
 
Peyo
Guest
Posts: n/a
 
      06-08-2007
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.
 
Reply With Quote
 
 
 
 
Joseph Kesselman
Guest
Posts: n/a
 
      06-08-2007
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
 
Reply With Quote
 
delirio
Guest
Posts: n/a
 
      06-08-2007
On Jun 8, 8:24 pm, Patrick <(E-Mail Removed)> wrote:
> 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>

 
Reply With Quote
 
Patrick
Guest
Posts: n/a
 
      06-11-2007
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

 
Reply With Quote
 
Patrick
Guest
Posts: n/a
 
      06-11-2007
delirio wrote:

> On Jun 8, 8:24 pm, Patrick <(E-Mail Removed)> wrote:
>
>>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

 
Reply With Quote
 
Patrick
Guest
Posts: n/a
 
      06-11-2007
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

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I simplify this code? James Yong Java 6 12-15-2006 09:59 PM
Adding constraints in Simplify and Altera Quartus Juan Carlos Allica VHDL 2 01-25-2006 12:28 PM
How do I Simplify a text array in a Struts form using a loop Dean Rettig Java 2 01-06-2004 08:56 PM
Can I simplify this? Steve Hémond Perl Misc 2 09-28-2003 04:02 PM
How to simplify logical expressions? Gerhard Rapp Java 2 07-30-2003 02:24 PM



Advertisments