RE: [xsl] template matching, passing parameters.

Subject: RE: [xsl] template matching, passing parameters.
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Tue, 30 Mar 2004 09:31:39 -0700
You would use xsl:with-param name="foo" select="bar"/>

For example, lets say youre a genealogist and you are trying to locate
the long lost last name of Mr. Foo.  You stumble across his unmarried
granddaughter who has kept the family name going for all these years.
You decide you want to go back and set the record straight with Mr. Foo.



Your family tree looks like this...
 
<foo>
	<child_of_foo>
		<foos_grandson/>
		<foos_granddaughter lastname="bar"/>
	</child_of_foo>
</foo>

Your templates to be able to perform such a thing would look like this.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:template match="/">
  <xsl:apply-templates select="//*[@lastname][1]" mode="find_last_name">
    <xsl:with-param name="the_whole_family_tree" select="*"/>
  </xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="find_last_name">
<xsl:param name="the_whole_family_tree"/>
  <xsl:apply-templates select="$the_whole_family_tree"
mode="return_to_your_roots">
    <xsl:with-param name="last_name" select="@lastname"/>
  </xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="return_to_your_roots">
<xsl:param name="last_name"/>
    <xsl:element name="{name()}">
      <xsl:attribute name="last_name"><xsl:value-of
select="$last_name"/></xsl:attribute>
      <xsl:apply-templates mode="return_to_your_roots">
      <xsl:with-param name="last_name" select="$last_name"/>
    </xsl:apply-templates>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

Which would result in this:

<?xml version="1.0" encoding="UTF-8"?>
<foo last_name="bar">
	<child_of_foo last_name="bar">
		<foos_grandson last_name="bar"/>
		<foos_granddaughter last_name="bar"/>
	</child_of_foo>
</foo>

Let me know if you have any more questions.

Best of luck!

<M:D/>

-----Original Message-----
From: poppe chris [mailto:pc_poppe@xxxxxxxxxxx] 
Sent: Tuesday, March 30, 2004 8:43 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] template matching, passing parameters.


Hello,

Im not sure how to explain my question, but i will give it a try.
Suppose you have a template wich matches with some elements of your 
sourcetree (of the input xml file). There you calculate something wich
you 
put in a variable or parameter. Now you want to pass this value to a 
template who matches with elements higher in the sourcetree then the 
previous elements. And then use that value at that (high) position.

Basically i want to pass a parameter from a template matching elements
deep 
in a tree to a template matching elements higher in the tree and also
change 
the current node to the higher element.
But Im not sure if this is possible.

I know that if you use call-template, you still stay at the same node
from 
the template where you perform the call so this is probably not the 
solution...

Any remarks or comments would be welcome,
sincerely,
Chris Poppe

_________________________________________________________________
Vraag van de week: Wat doe jij tegen spam, ongewenste e-mail? 
http://www.msn.be/microsoft/potential/default.asp

Current Thread