can I simplify this?

Discussion in 'XML' started by Patrick, Jun 8, 2007.

  1. Patrick

    Patrick Guest

    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
     
    Patrick, Jun 8, 2007
    #1
    1. Advertising

  2. Patrick

    Peyo Guest

    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.
     
    Peyo, Jun 8, 2007
    #2
    1. Advertising

  3. 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
     
    Joseph Kesselman, Jun 8, 2007
    #3
  4. Patrick

    delirio Guest

    On Jun 8, 8:24 pm, Patrick <> 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>
     
    delirio, Jun 8, 2007
    #4
  5. Patrick

    Patrick Guest

    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
     
    Patrick, Jun 11, 2007
    #5
  6. Patrick

    Patrick Guest

    delirio wrote:

    > On Jun 8, 8:24 pm, Patrick <> 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
     
    Patrick, Jun 11, 2007
    #6
  7. Patrick

    Patrick Guest

    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
     
    Patrick, Jun 11, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juan Carlos Allica
    Replies:
    2
    Views:
    677
    Noway2
    Jan 25, 2006
  2. Gerhard Rapp

    How to simplify logical expressions?

    Gerhard Rapp, Jul 29, 2003, in forum: Java
    Replies:
    2
    Views:
    1,229
    Peter Schoaff
    Jul 30, 2003
  3. Dean Rettig
    Replies:
    2
    Views:
    7,755
    Oscar Kind
    Jan 6, 2004
  4. James Yong

    Can I simplify this code?

    James Yong, Dec 6, 2006, in forum: Java
    Replies:
    6
    Views:
    319
    Tris Orendorff
    Dec 15, 2006
  5. Steve Hémond

    Can I simplify this?

    Steve Hémond, Sep 27, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    88
    Frank Maas
    Sep 28, 2003
Loading...

Share This Page