[xsl] Passing parameters using <xsl:apply-templates>

Subject: [xsl] Passing parameters using <xsl:apply-templates>
From: Mark <charltonrainbird@xxxxxxxxxxxxxx>
Date: Sat, 16 Feb 2008 10:42:16 +0000
Hi

I'm a long-time reader but this is the first time I've posted so
please bear with me if I've forgotten something or not made myself
clear.

I'm using XSLT 2 with Saxon 8.8 run from the command line.

I'm inputting an xml file that lists the xml files to be processed to
then iterate through that list using document() function:

<?xml version="1.0" encoding="UTF-8"?>
<masterlist>
 <docsinc filename="param-test.xml"/>
<!--<docsinc filename="wdobatwn.xml"/>
<docsinc filename="wdobavit.xml"/>-->
</masterlist>

I'm attempting to pass parameters that are specified in my templates
to the output documents, based on finding the document-uri for the
specific output xml. The templates will use the original input
document-uri unless I use <xsl:with-param> to reset them. This is so
my output files can be named using the prefix of the file they are
derived from.

Everything I can find on this I understand to tell me that I need to
pass the parameter through each element level of the processed xml
data otherwise that parameter passes no further than the last element
level it is set at. I am attempting this by setting the parameter
*intrefNow* in every template which I believe is therefore required.

However, my output shows that *intrefNow* is resetting to the output
filename down to a certain level and then stopping, reverting to the
original input filename. I've put text output markers into the
templates to indicate what the parameter is set at for each template
output. My problem is that I cannot understand why it passes no
further.

I've greatly reduced my original xsl and merged the templates but need
to maintain some complexity to fully illustrate my problem:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns="http://www.w3.org/1999/xhtml";>
 <xsl:output method="xhtml" doctype-public="-//W3C//DTD XHTML 1.0
Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
omit-xml-declaration="yes" indent="no" encoding="iso-8859-1"
media-type="text/html"/>

 <xsl:template match="/">
 <xsl:message>Pub Title is <xsl:value-of select="$pub_title"/></xsl
:message>

 <xsl:for-each select="document(//@filename)">
 <xsl:variable name="pub_title"><xsl:value-of
select="document-uri(.)"/></xsl:variable>
 <xsl:message>Current file = <xsl:value-of
select="document-uri(.)"/></xsl:message>

<?outputs the product INDEX pages?>
 <xsl:for-each select="//level[@tag = 'finding_aids']//letter">
 <xsl:variable name="fileNm" select="title/ttxt/text()"/>
 <xsl:result-document href="{$pub_title}_{$fileNm}.htm">
 <html xmlns="http://www.w3.org/1999/xhtml";>
  <h1><xsl:value-of select="title/ttxt/text()"/></h1>
   <xsl:apply-templates>
   <xsl:with-param name="intrefNow" select="$pub_title"/>
  </xsl:apply-templates>/>
 </html>
 </xsl:result-document>
 </xsl:for-each>

 </xsl:for-each>
</xsl:template>

<?Variables =============================================?>

 <xsl:variable name="pub_title"><xsl:value-of
select="document-uri(.)"/></xsl:variable>

<?Templates =============================================?>

 <xsl:template match="level"><xsl:param name="intrefNow"
select="$pub_title"/><xsl:text>1. </xsl:text><xsl:value-of
select="node-name(.)"/><xsl:text> - </xsl:text><xsl:value-of
select="$intrefNow"/><xsl:apply-templates/></xsl:template>

 <xsl:template match="para">
 <xsl:param name="intrefNow" select="$pub_title"/><xsl:text>2.
</xsl:text><xsl:value-of select="node-name(.)"/><xsl:text> -
</xsl:text><xsl:value-of select="$intrefNow"/>
 <p><xsl:apply-templates/></p>
 </xsl:template>

 <xsl:template match="letter/title"/>

 <xsl:template match="refs"><xsl:param name="intrefNow"
select="$pub_title"/><xsl:text>4. </xsl:text><xsl:value-of
select="node-name(.)"/><xsl:text> - </xsl:text><xsl:value-of
select="$intrefNow"/><xsl:apply-templates/></xsl:template>

 <xsl:template match="//intref">
 <xsl:param name="intrefNow" select="$pub_title"/><xsl:text>8.
</xsl:text><xsl:value-of select="node-name(.)"/><xsl:text> -
</xsl:text><xsl:value-of select="$intrefNow"/>
  <a>
  <xsl:attribute name="href">
  <xsl:text disable-output-escaping="yes">#!-- #ID=</xsl:text>
   <xsl:value-of select="concat($intrefNow, '_', @idref)"/>
   <xsl:text disable-output-escaping="yes"> --#</xsl:text>
  </xsl:attribute>
  <xsl:value-of select="."/>
 </a>
 </xsl:template>

 <xsl:template match="index"><xsl:param name="intrefNow"
select="$pub_title"/><xsl:text>9. </xsl:text><xsl:value-of
select="node-name(.)"/><xsl:text> - </xsl:text><xsl:value-of
select="$intrefNow"/><xsl:apply-templates/></xsl:template>
 <xsl:template match="letter"><xsl:param name="intrefNow"
select="$pub_title"/><xsl:text>10. </xsl:text><xsl:value-of
select="node-name(.)"/><xsl:text> - </xsl:text><xsl:value-of
select="$intrefNow"/><xsl:apply-templates/></xsl:template>

 <xsl:template match="topic">
 <xsl:param name="intrefNow" select="$pub_title"/><xsl:text>11.
</xsl:text><xsl:value-of select="node-name(.)"/><xsl:text> -
</xsl:text><xsl:value-of select="$intrefNow"/>
 <p><xsl:apply-templates/></p>
 </xsl:template>

 <xsl:template match="tptxt"><xsl:param name="intrefNow"
select="$pub_title"/><xsl:text>12. </xsl:text><xsl:value-of
select="node-name(.)"/><xsl:text> - </xsl:text><xsl:value-of
select="$intrefNow"/><xsl:apply-templates/></xsl:template>

 <xsl:template match="topic1 | c-topic">
 <xsl:param name="intrefNow" select="$pub_title"/><xsl:text>13.
</xsl:text><xsl:value-of select="node-name(.)"/><xsl:text> -
</xsl:text><xsl:value-of select="$intrefNow"/>
 <p class="indent10bl"><xsl:apply-templates/></p>
 </xsl:template>

 <xsl:template match="text">
 <xsl:param name="intrefNow" select="$pub_title"/><xsl:text>15.
</xsl:text><xsl:value-of select="node-name(.)"/><xsl:text> -
</xsl:text><xsl:value-of select="$intrefNow"/>
 <xsl:apply-templates/>
 </xsl:template>

<?Inline styles?>
<?_ =============================================?>
 <xsl:template match="ttxt">
 <xsl:param name="intrefNow" select="$pub_title"/><xsl:text>16.
</xsl:text><xsl:value-of select="node-name(.)"/><xsl:text> -
</xsl:text><xsl:value-of select="$intrefNow"/><xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="b">
 <xsl:param name="intrefNow" select="$pub_title"/><xsl:text>19.
</xsl:text><xsl:value-of select="node-name(.)"/><xsl:text> -
</xsl:text><xsl:value-of
select="$intrefNow"/><strong><xsl:apply-templates/></strong>
 </xsl:template>

</xsl:stylesheet>

====================================

XML data is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE elecbook PUBLIC "-//CCH//DTD GEP Book//EN" "gep.dtd">

<elecbook header="Doing Business in Hong Kong Commentary"
footer="Copyright 2007, CCH Asia Ltd" type="comm" country="as">
<title style="left"><ttxt>Doing Business in Hong Kong Commentary</ttxt></title>
 <level tag="finding_aids" id="io89731.sl2102181">
<title style="left"><ttxt>Finding Aids</ttxt></title>
<index id="io89733.sl2100753" tag="topical_index">
 <title style="left"><ttxt>INDEX</ttxt></title>
<letter tag="words">
<title tag="words" style="left"><ttxt>A</ttxt></title>
 <topic tag="topic">
<tptxt tag="words"><b>Accelerated depreciation allowance</b></tptxt>
<refs tag="comm"><intref idref="io89488.sl2099018">HKG
&para;35-513</intref></refs></topic>
 <topic tag="topic">
<tptxt tag="words"><b>Assessable income</b></tptxt>
<topic1 tag="topic_1">
<tptxt tag="words">corporations</tptxt>
 <refs tag="comm"><intref idref="io89459.sl2098819">HKG
&para;35-101</intref></refs></topic1></topic>
</letter>
<letter tag="words">
 <title tag="words" style="left"><ttxt>Y</ttxt></title>
<topic tag="topic">
<tptxt tag="words"><b>Year of account</b></tptxt>
 <refs tag="comm"><intref idref="io89456.sl2098796">HKG
&para;35-004</intref></refs></topic></letter></index></level></elecbook>


Thanks for any clarifications that anyone can offer.

Regards

Mark

Current Thread