Global variable not working in my XSL

P

PatrickRThomas

his question has probably been asked a million times, but I can't get
this to work.

I'm trying to create a global variable that contains some HTML so that
I can reference it from multiple templates. For some reason, the
templates don't seem to be able to see this variable. What am I doing
wrong? Here is a snip of my code:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="MasterSpacer">
<tr>
<td height="20"/>
</tr>
</xsl:variable>

<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css"
href="/stylesheet/MyStyles.css"/>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" align="center">
<xsl:apply-templates select="data/header"/>
<xsl:apply-templates select="data/body"/>
</table>
</body>
</html>
</xsl:template>

....

<xsl:template match="body">
<xsl:copy-of select="$MasterSpacer"/>

....
 
D

David Carlisle

What am I doing
wrong? Here is a snip of my code:

As far as I can see, whatever you are doing wrong, it must be in the
part of your code that was snipped.

David
 
P

PatrickRThomas

Here is the complete XSL with XML to test with it. Based on what I've
read, this should work.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="MasterSpacer">
<tr>
<td height="20"/>
</tr>
</xsl:variable>
<!-- The root of the data xml -->
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css"
href="/stylesheet/MyStyles.css"/>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" align="center">
<xsl:apply-templates select="data/header"/>
<xsl:apply-templates select="data/body"/>
</table>
</body>
</html>
</xsl:template>
<!-- The criteria section of the report. -->
<xsl:template match="header">
<table border="1" cellpadding="1" cellspacing="1" align="center">
<tr>
<th align="center" colspan="2">My Header</th>
</tr>
<tr>
<td align="left" width="100">
<b>Username</b>
</td>
<td align="left" width="100">
<xsl:value-of select="username"/>
</td>
</tr>
<tr>
<td align="left">
<b>Report Date</b>
</td>
<td align="left">
<xsl:value-of select="report_date"/>
</td>
</tr>
</table>
</xsl:template>
<!-- The main body of the document -->
<xsl:template match="body">
<xsl:copy-of select="$MasterSpacer"/>
<tr>
<td>
<table border="1" cellpadding="1" cellspacing="1" align="center">
<tr>
<th align="center" colspan="9">Document Body</th>
</tr>
</table>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>


<?xml version="1.0" encoding="UTF-8"?>
<data>
<header>
<username>pthomas</username>
<report_date>05/20/2005</report_date>
</header>
<body/>
</data>
 
D

David Carlisle

saxon makes the variable content appear (I added the %%%%%%% later:)

I think your code is correct and your system is less so.



David


$ saxon gv.xml gv.xsl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link rel="stylesheet" type="text/css" href="/stylesheet/MyStyles.css">
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" align="center">
<table border="1" cellpadding="1" cellspacing="1" align="center">
<tr>
<th align="center" colspan="2">My Header</th>
</tr>
<tr>
<td align="left" width="100"><b>Username</b></td>
<td align="left" width="100">pthomas</td>
</tr>
<tr>
<td align="left"><b>Report Date</b></td>
<td align="left">05/20/2005</td>
</tr>
</table>
<tr>
<td height="20"></td>%%%%%%%%%%%%%%%%%%%%%%%%%%%%
</tr>
<tr>
<td>
<table border="1" cellpadding="1" cellspacing="1" align="center">

<tr>
<th align="center" colspan="9">Document Body</th>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top