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
do)

<?xml version="1.0" encoding="iso-8859-1"?>
<cars>
	<mercedes>
		<mercedesA>blaA</mercedesA>
		<mercedesB>blaB</mercedesB>
	</mercedes>
	<ford>
		<fordA>huhA</fordA>
		<fordB>huhB</fordB>
		<fordC>huhC</fordC>
	</ford>
</cars>

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:template match="cars">
		<html>
			<head>
				<title>Cars</title>
			</head>
			<body>
				<xsl:apply-templates select="node()"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="ford">
		<table>
			<tbody>
				<tr>
					<td colspan="2">A FORD CARD</td>
				</tr>
				<tr>
					<td>Ford specific A:</td>
					<td><xsl:value-of
select="fordA"></xsl:value-of></td>
				</tr>
				<tr>
					<td>Ford specific B:</td>
					<td><xsl:value-of
select="fordB"></xsl:value-of></td>
				</tr>
				<tr>
					<td>Ford specific C:</td>
					<td><xsl:value-of
select="fordC"></xsl:value-of></td>
				</tr>
			</tbody>
		</table>
	</xsl:template>
	<xsl:template match="mercedes">
		<table>
			<tbody>
				<tr>
					<td colspan="2">A MERCEDES CAR</td>
				</tr>
				<tr>
					<td>Mercedes specific A</td>
					<td><xsl:value-of
select="mercedesA"></xsl:value-of></td>
				</tr>
				<tr>
					<td>Mercedes specific B</td>
					<td><xsl:value-of
select="mercedesB"></xsl:value-of></td>
				</tr>
			</tbody>
		</table>
	</xsl:template>
</xsl:stylesheet>

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:
	<car>
	<title>
		A FORD CAR
	</title>
	<information>
		<row>
			<title>Ford specific A</title>
			<text><xsl:value-of select="fordA"></text>
		</row>
		<row>..</row>
	</information>
	</car>

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

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

Thanks again,

Peter

-----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" />
  </td>
</xsl:template>

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.
    </xsl:with-param>
  </xsl:call-template>

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.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


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


Current Thread