xsl-list
[Top] [All Lists]

Re: [xsl] modify output document elements

2020-03-05 02:44:02
Well, many things...

First, using named templates is often a bad practise. You'd better use templates with patterns, it makes the stylesheet more readable, more maintenable, and it allows to process source in many steps.

If you want modify your generated table, the best thing to do is to reprocess it :

<xsl:mode name="tableModif" on-no-match="shallow-copy"/>
<xsl:template....>
  <xsl:variable name="firstStep" as="element(table)">
    <table border="0" cellspacing="0">
      <!-- your whole table code here -->
    </table>
  </xsl:variable>
  <xsl:apply-templates select="$firstStep" mode="tableModif"/>
</xsl:template>

<xsl:template match="th" mode="tableModif">
  <xsl:copy>
    <xsl:attribute name="style">font-weight: bold;</xsl:attribute>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

Here two things : when table element is generated, it is kept into a variable, and does not do to output. Then, I re-process the variable (that contains a table element) in a user-defined mode, call tableModif ; in that mode, when there is no specific template that match an item, item is copied. And I declare a template that is activated for each th element in mode tableModif, and this template  copies the template element, adds a style attribute, and continue to process each attributes and each child elements of th - there are copied, because of xsl:mode definition, and because there is no specific tempalte rule.

Hope this helps,
Christophe

Le 04/03/2020 à 18:49, Kammering, Raimund raimund(_dot_)kammering(_at_)desy(_dot_)de a écrit :
Hello,

I have a stylesheet which creates a HTML output document dynamically based on 
the incoming XML file. I use some for-each loop to get the processing done in 
the right order. My layout causes the stylesheet to look like this:

   <xsl:template name="details_table">
     <table border="0" cellspacing="0">
       <tr>
         <td width="64px">
           <a href="#" onclick="toggle('{$details_table}')" title="show/hide 
details">
             <img src="show_hide.png"/>
           </a>
         </td>
         <td>
           Details
         </td>
       </tr>
       <table id="{$details_table}" cellspacing="3" style="display: block">
         <tr>
           <td width="64px">
             <img border="0" src="{$imagedir}/null.gif"/>
           </td>
           <th align="left">Start</th>
           <th align="left">End</th>
           <th align="left">Duration [h]</th>
           <th align="left">Type</th>
           <th align="left">Category</th>
           <th align="left">Comment</th>
         </tr>
<xsl:for-each select="entry[severity='STATISTICS']/statistics_category">
           ...
           <xsl:choose>
             <xsl:when test=".='Down'">
               <td width="150" valign="top">
                 <xsl:choose>
                   <xsl:when test="../Down='not_set'">
                      <font color="red">not_set</font>
                   </xsl:when>
                   <xsl:otherwise>
                      <xsl:value-of select="../Down"/>
                   </xsl:otherwise>
                 </xsl:choose>
               </td>
             </xsl:when>
             ...
         </xsl:for-each>
       </table>
     </table>
   </xsl:template>

Now I would like to modify the style of the details_table depending on the 
outcome of the test within the for-each loop. Since the table element has 
already been created, I do not know how one could do this without changing the 
overall logic. Is there a way to access the elements of the created HTML DOM 
tree from within the stylesheet? Or am I hunting in the wrong direction?!

Any ideas/help welcome - greetings,
Raimund Kammering

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>