This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: xsl:variable and match
- To: xsl-list at mulberrytech dot com
- Subject: Re: xsl:variable and match
- From: "G. Ken Holman" <gkholman at CraneSoftwrights dot com>
- Date: Fri, 12 May 2000 18:19:01 +0200
- Reply-To: xsl-list at mulberrytech dot com
At 00/05/11 18:48 -0500, Ross Cormier wrote:
>Can I use a variable in the match part of xsl:template?
>
>
><xsl:variable name="x" select="/ROWSET/REPLACE/@EL"/>
><xsl:template match="/ROWSET/{$x}">
> <NEWELEMENT><xsl:apply-templates/></NEWELEMENT>
></xsl:template>
Although you can't use it the way you have indicated above, it isn't a
blanket truth that a variable name cannot be used at all in the match part
of a template.
Consider the example below where a top-level variable (the only variables
that can be referenced in template match expressions), parameterized from
the command line, is used to match only those elements named by the
operator invoking the stylesheet.
Note that while variables cannot be used as a node test in a location step,
they can be used in a predicate.
Interestingly, Saxon gives an error on this stylesheet that works fine with
XT. I think Saxon is in error here because XSLT [5] allows predicates in
match patterns.
I hope this helps.
............. Ken
T:\ftemp>type test.xml
<?xml version="1.0"?>
<test>
<a>This is a</a>
<b>This is b</b>
<c>This is c</c>
<d>This is d</d>
</test>
T:\ftemp>type test.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:param name="which"/> <!--require the operator to specify-->
<!--push all children for illustration-->
<xsl:template match="/test">
<xsl:apply-templates/>
</xsl:template>
<!--only interested in the ones indicated by the operator-->
<xsl:template match="*[name(.)=$which]" priority="1">
<xsl:value-of select="."/>
</xsl:template>
<!--eat all other output-->
<xsl:template match="*|text()" priority="0"/>
</xsl:stylesheet>
T:\ftemp>xt test.xml test.xsl which=a
This is a
T:\ftemp>xt test.xml test.xsl which=c
This is c
T:\ftemp>
--
G. Ken Holman mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
Web site: XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath ISBN 1-894049-04-7
Next instructor-led training: 2000-05-11/12,2000-05-15,
- 2000-06-12,2000-06-13,2001-01-27
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list