RE: [xsl] making trees/lists during execution (dynamically)

Subject: RE: [xsl] making trees/lists during execution (dynamically)
From: "Peter Billen" <peter@xxxxxxxxxxx>
Date: Mon, 9 Feb 2004 17:32:00 +0100
Thanks a lot for your input, I think this might help me, but I'm not sure
yet. I have created a small .xml file as example.

Imagine the following .xml file (yes it's a stupid example, but this will

<?xml version="1.0" encoding="iso-8859-1"?>

It contains a list of branch of cars, each with its own data (note mercedesA
contains different information than fordA).

Now I want to transform this .xml file into a .html file, with the following
.xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	<xsl:template match="cars">
				<xsl:apply-templates select="node()"/>
	<xsl:template match="ford">
					<td colspan="2">A FORD CARD</td>
					<td>Ford specific A:</td>
					<td>Ford specific B:</td>
					<td>Ford specific C:</td>
	<xsl:template match="mercedes">
					<td colspan="2">A MERCEDES CAR</td>
					<td>Mercedes specific A</td>
					<td>Mercedes specific B</td>

It's a list of table, one for each car.

As you notice, the html (table, tr, td, ..) stuff in the two templates is
EXACT the same!

So now my question: I'd like to do something as the following:

<xsl:template match="ford">
	create a list/tree, which will look like the following:
			<title>Ford specific A</title>
			<text><xsl:value-of select="fordA"></text>

	<xsl:call-template name="outputTable>
		<xsl:with-param name="car" select="my list">
<xsl:template match="Mercedes>
	same as template for ford

<xsl:template name="outputTable">
	loop through the parameter "car" and output it

Thanks again,


-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Jeni Tennison
Sent: maandag 9 februari 2004 17:03
To: Peter Billen
Cc: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] making trees/lists during execution (dynamically)

Hi Peter,

> Is it possible to make trees (or lists) dynamically during the
> transformation?

You can build a "result tree fragment", which you assign to a variable
or parameter, and can then copy into the result with <xsl:copy-of>. As
a really simple example, say you wanted to create table cells using a
common template (because the HTML for the table cells was complex in
some way). The content of the table cell needs to be passed in as a
result tree fragment using a parameter, and then copied into the cell
that you create.

The create-table-cell template might look like:

<xsl:template name="create-table-cell">
  <xsl:param name="content" />
  <td ...>
    <xsl:copy-of select="$content" />

To call it, you'd do something like:

  <xsl:call-template name="create-table-cell">
    <xsl:with-param name="content">
      Various content, <em>including</em> markup.

The $content parameter here gets passed a result tree fragment: a
small tree, destined for the result, that contains text nodes and
an <em> element. Note that if you use <xsl:value-of> on $content,
you'll get the string value of the result tree fragment, and lose any
markup that it contains.

The one limitation about result tree fragments is that you can't
process them without converting them to node sets. To do that, you
need to use an extension function such as exsl:node-set(). From your
brief description, that might be a problem, or it might be that you
just need to think about what you're trying to do in a different way.

> If needed, I can provide an example, but I hope this is clear
> enough.

If the above doesn't hit the spot, an example would help us understand
what you need to do.



Jeni Tennison

 XSL-List info and archive:

 XSL-List info and archive:

Current Thread