[xsl] Repetitive variable definitions
2007-09-14 10:24:54
I have a question of a general nature concerning variables and xsl
templates. I like to break my XSL down into templates as I find them
much easier to work with, but I find it very limiting when I have to
keep redefining the same variables over and over in different templates
with different contexts.
In my very simplistic example, I have had to define a variable
"noofcols" once in each template for a total of 3 times. I would love to
only define it once and have it "cascade" down the child templates
without making the XSL a single template. I don't think this is
possible, but you never know until you ask.
So my question is - can I define the "noofcols" only once whether
variable, parameter template or any other method, or am I stuck with
defining it as shown?
Thanks in advance,
Dan Whitney
XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<example>
<table desc="Example: 1 col table">
<tr>
<td col="1">col 1</td>
</tr>
</table>
<table desc="Example: 2 col table">
<tr>
<td col="1">col 1</td>
<td col="2">col 2</td>
</tr>
</table>
<table desc="Example: 3 col table">
<tr>
<td col="1">col 1</td>
<td col="2">col 2</td>
<td col="3">col 2</td>
</tr>
</table>
</example>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="table">
<xsl:variable name="noofcols" select="tr/td[last()]/@col"/>
<table border="1">
<tr>
<td align="center" colspan="{$noofcols}">
<xsl:value-of select="@desc"/>
</td>
</tr>
<xsl:apply-templates select="tr"/>
</table>
</xsl:template>
<xsl:template match="tr">
<xsl:variable name="noofcols"
select="self::tr[1]/td[last()]/@col"/>
<tr>
<xsl:choose>
<xsl:when test="$noofcols = 1">
<xsl:attribute name="bgcolor">red</xsl:attribute>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="td"/>
</tr>
</xsl:template>
<xsl:template match="td">
<xsl:variable name="noofcols"
select="parent::tr[1]/td[last()]/@col"/>
<td>
<xsl:choose>
<xsl:when test="$noofcols = 3">
<xsl:attribute name="bgcolor">blue</xsl:attribute>
</xsl:when>
</xsl:choose>
td col variable = <xsl:value-of select="$noofcols"/>
</td>
</xsl:template>
</xsl:stylesheet>
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
<Prev in Thread] |
Current Thread |
[Next in Thread>
|
- [xsl] merging xml's using XSLT, merge.xslt, Senthil Nathan
- RE: [xsl] merging xml's using XSLT, merge.xslt, Senthil Nathan
- RE: [xsl] merging xml's using XSLT, merge.xslt, Michael Kay
- Re: [xsl] merging xml's using XSLT, merge.xslt, Senthil Nathan
- Re: [xsl] merging xml's using XSLT, merge.xslt, Abel Braaksma
- Re: [xsl] merging xml's using XSLT, merge.xslt, Senthil Nathan
- Re: [xsl] merging xml's using XSLT, merge.xslt, Abel Braaksma
- Re: [xsl] merging xml's using XSLT, merge.xslt, Senthil Nathan
- [xsl] Repetitive variable definitions,
Whitney, Dan \(CanWest Interactive\) <=
- RE: [xsl] Repetitive variable definitions, Bjorndahl, Brad
- Re: [xsl] merging xml's using XSLT, merge.xslt, Abel Braaksma
- Message not available
- Message not available
- Message not available
- Re: [xsl] merging xml's using XSLT, merge.xslt, Senthil Nathan
- Re: [xsl] merging xml's using XSLT, merge.xslt, Abel Braaksma
- Re: [xsl] merging xml's using XSLT, merge.xslt, Senthil Nathan
- Re: [xsl] merging xml's using XSLT, merge.xslt, Abel Braaksma
- Re: [xsl] merging xml's using XSLT, merge.xslt, Owen Rees
|
|
|