Re: [xsl] modify output document elements

Subject: Re: [xsl] modify output document elements
From: "Christophe Marchand cmarchand@xxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 5 Mar 2020 08:43:55 -0000
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"/>
B  <xsl:variable name="firstStep" as="element(table)">
B B B  <table border="0" cellspacing="0">
B B B B B  <!-- your whole table code here -->
B B B  </table>
B  </xsl:variable>
B  <xsl:apply-templates select="$firstStep" mode="tableModif"/>

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

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 templateB 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,

Le 04/03/2020 C 18:49, Kammering, Raimund raimund.kammering@xxxxxxx a C)critB :

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">
<td width="64px">
<a href="#" onclick="toggle('{$details_table}')" title="show/hide details">
<img src="show_hide.png"/>
<table id="{$details_table}" cellspacing="3" style="display: block">
<td width="64px">
<img border="0" src="{$imagedir}/null.gif"/>
<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>
<xsl:for-each select="entry[severity='STATISTICS']/statistics_category">
<xsl:when test=".='Down'">
<td width="150" valign="top">
<xsl:when test="../Down='not_set'">
<font color="red">not_set</font>
<xsl:value-of select="../Down"/>

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

Current Thread