RE: [xsl] Transforming a node in XSL multiple times

Subject: RE: [xsl] Transforming a node in XSL multiple times
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Tue, 29 Oct 2002 10:40:16 -0500
[ Spencer]

>  We have a tag like this <img src="hello.bmp"/> and we want 
> to perform  multiple transformations on it such as adding alt 
> and width  attributes.  This can be done in one template quite easily.
> However, the problem arises if we need to change this tag 
> again but  through a different Xpath.  For example, we do a 
> template match on img  and add alt and width attributes.  
> Then we do a template match on the  div and because it has a 
> center alignment, we now want to add the  height attribute to 
> the preceeding image using the xpath  preceding::img
>  <img src="hello.bmp"/>
>  <div align="center"/>

Usually, there is another way to handle these tasks.  You just have to
approach it differently.  Instead of multiple passes, it is ususally
better to think in terms of conditions - conditions for selecting sets
of nodes, and conditions for handling them.   And remember that any node
is accessible from any context, as long as you can think of the right
condition to express its location (relative or absolute).

Mike Kay said 

"Or you can do it in a single pass, in which case each node in the
result tree must be computed as a function of the things in the source
tree that it depends on. You can visit a source node as often as you
like, but you can only visit a result node once."

Applying this notion to your particular example, you want to change the
attributes depending on the next following "div" element.  You can
actually say that fairly directly:

	This assumes that the "div" in question is supposed to be the
very next sibling element of the context node.  It also assumes that the
source element for the image has an attribute names "height".  If not,
just substitute the right expression for the height.
<img src='hello.bmp'>
	<xsl:if test='following-sibling::*[1][name()="div" and
		<xsl:attribute name='height'><xsl:value-of

The key is to figure out what condition will select or specify the right
nodes, then you can do almost anything in xslt.


Tom P

 XSL-List info and archive:

Current Thread