RE: [xsl] Is it possible to access a tag after using apply-templates?

Subject: RE: [xsl] Is it possible to access a tag after using apply-templates?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 4 Jul 2008 15:40:32 +0100
Another way of tackling this kind of problem, which I've generally found
works better in the long run, is as follows. At present you essentially have
a miniature layout language, and you have written an interpreter in XSLT
that interprets that language, fetching data from a source document when the
layout script instructs you to do so. 

The alternative approach is to compile your layout language: that is, to
write an XSLT stylesheet that converts it into an XSLT stylesheet, which
then operates on the data file directly.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: XemonerdX [mailto:xemonerdx@xxxxxxxxx] 
> Sent: 04 July 2008 14:56
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Is it possible to access a tag after using 
> apply-templates?
> 
> Is the following possible? I can't seem to wrap my head 
> around it, but I am an XSLT newbie, so that might explain it :)
> 
> I have an XML file that contains data that needs to be displayed:
> <?xml version="1.0" ?>
> <top>
> 	<data>
> 		<title>title of page</title>
> 		<name>Your name</name>
> 	</data>
> 	<cells>
> 		<cell>
> 			<name>cell 1</name>
> 			<value>100</value>
> 		</cell>
> 		<cell>
> 			<name>cell 2</name>
> 			<value>200</value>
> 		</cell>
> 		<cell>
> 			<name>cell 3</name>
> 			<value>300</value>
> 		</cell>
> 	</cells>
> </top>
> 
> I also have a layout XML file that will be used to put the 
> data from the above XML-file into a certain layout, called layout.xml:
> <?xml version="1.0" encoding="utf-8"?>
> <layout>
> 	<layout-main>
> <html>
> <head>
> 	<title><insert-title/></title>
> </head>
> <body>
> 	<div>
> 		My name is: <insert-name/>
> 	</div>
> 	<div>
> 		<table>
> 			<insert-cells/>
> 		</table>
> 	</div>
> </body>
> </html>
> 	</layout-main>
> 	<layout-cell>
> 		<tr>
> 			<td><insert-cell-name/></td>
> 			<td><insert-cell-value/></td>
> 		</tr>
> 	</layout-cell>
> </layout>
> 
> And then I have the XSL file to tie it all together (hopefully!):
> <?xml version="1.0"?>
> <xsl:stylesheet 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
> version="1.0"> <xsl:output method="html" indent="yes"/> 
> <xsl:variable name="data" select="/"/> <xsl:variable name="layout"
> select="document('http://localhost/webopac2/xml/datatemplate_l
> ayout.xml')"/>
> <xsl:template match="/">
> 	<xsl:apply-templates select="$layout/layout/layout-main/*"/>
> </xsl:template>
> <!-- transformations -->
> <xsl:template match="insert-name">
> 	<xsl:value-of select="$data/top/data/name"/> 
> </xsl:template> <xsl:template match="insert-title">
> 	<xsl:value-of select="$data/top/data/title"/> 
> </xsl:template> <xsl:template match="insert-cells">
> 	<xsl:for-each select="$data/top/cells/cell">
> 		<xsl:variable name="name" select="name"/>
> 		<xsl:variable name="value" select="value/*"/>
> 		<xsl:apply-templates 
> select="$layout/layout/layout-cell/tr">
> 			<xsl:with-param name="name" select="$name"/>
> 		</xsl:apply-templates>
> 		<xsl:call-template name="insert-cell-name">
> 			<xsl:with-param name="name" select="$name"/>
> 		</xsl:call-template>
> 	</xsl:for-each>
> </xsl:template>
> <xsl:template name="insert-cell-name">
> 	<xsl:param name="name"/>
> 	<xsl:value-of select="$name"/>
> </xsl:template>
> <!-- Identity transformation -->
> <xsl:template match="@*|node()">
>     <xsl:copy>
>         <xsl:apply-templates select="@*|node()"/>
>     </xsl:copy>
> </xsl:template>
> </xsl:stylesheet>
> 
> I would like the output to look like the following:
> <html>
> <head>
> 	<title>title of page</title>
> </head>
> <body>
> 	<div>
> 		My name is: Your name
> 	</div>
> 	<div>
> 		<table>
> 			<tr>
> 				<td>cell 1</td>
> 				<td>100</td>
> 			</tr>
> 			<tr>
> 				<td>cell 2</td>
> 				<td>200</td>
> 			</tr>
> 			<tr>
> 				<td>cell 3</td>
> 				<td>300</td>
> 			</tr>
> 		</table>
> 	</div>
> </body>
> </html>
> 
> Unfortunately that's not quite the result. I can't seem to be 
> able to replace the 'insert-cell-name' and 
> 'insert-cell-value' tags with the corresponding 'name' and 
> 'value' tag values. Is this possible?
> 
> The reason I want to keep as much layout out of the XLS-file 
> as possible is to allow non-developers to design a layout 
> without knowing XSLT. This way I can hopefully also use a 
> single XLS-file where 'layout.xml' can be easily changed/generated.
> 
> Thanxxx for any guidance/advice/comments...
> 
> Edwin

Current Thread