[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Transforming a node in XSL multiple times

Subject: RE: Transforming a node in XSL multiple times
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Tue, 29 Oct 2002 10:40:16 -0500
xsl multiple passes
[ 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
@align="center"]'>
		<xsl:attribute name='height'><xsl:value-of
select='@height'/></xsl:attribute>
	</xsl:if>
</img>

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

Cheers,

Tom P

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.