xsl-list
[Top] [All Lists]

RE: [xsl] Repetitive variable definitions

2007-09-14 14:25:41
Hi,

This is also hasty. You don't need "three different variables". You need a new 
variable each time you invoke the 'table' template.

Brad


-----Original Message-----
From: cknell(_at_)onebox(_dot_)com [mailto:cknell(_at_)onebox(_dot_)com]
Sent: September 14, 2007 4:51 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Repetitive variable definitions

(Picture that I now have my right hand over my heart.)

I promise that I will never give a hasty answer again.

I did not notice that your input document had multiple table elements. I was 
working under the impression that there was only one, and based my answer on 
that.

If you indeed have three sibling table elements, then there are going to be 
three <td> elements matching the XPath "table/tr/td[last()]".

If that is true then there is no way that you can have one variable to hold 
three different values. (Certainly not in XSLT/XPath 1.0).

Given the example XML input document, you will necessarily have to create three 
different variables to hold the three values you want to capture.

If you want to re-think your example input document, I promise to make a 
thorough examination of it before responding again.
--
Charles Knell
cknell(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     Whitney, Dan \(CanWest Interactive\) <DWhitney(_at_)canwest(_dot_)com>
Sent:     Fri, 14 Sep 2007 14:08:26 -0500
To:       <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject:  RE: Re: [xsl] Repetitive variable definitions

Maybe I'm missing something here, but when I try your example with a slightly 
revised stylesheet I now get noofcols var always = 1

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<!-- your example -->
        <xsl:variable name="noofcols"
select="/example/table/tr/td[last()]/@col"/>

        <xsl:variable name="bg-color">
            <xsl:choose>
                <xsl:when test="$noofcols = 1">
                    <xsl:value-of select="'red'"/>
                </xsl:when>
                <xsl:when test="$noofcols = 3">
                    <xsl:value-of select="'blue'"/>
                </xsl:when>
            </xsl:choose>
        </xsl:variable>

 <!-- and added this -->
    <xsl:template match="example">
        <xsl:apply-templates/>
    </xsl:template>


    <xsl:template match="table">
        <p><xsl:value-of select="$noofcols"/></p>
        <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">
        <tr bgcolor="{$bg-color}">
            <xsl:apply-templates select="td"/>
        </tr>
    </xsl:template>


    <xsl:template match="td">
        <td bgcolor="{$bg-color}">
            td col variable = <xsl:value-of select="$noofcols"/>
        </td>
    </xsl:template>

</xsl:stylesheet>



________________________________

Daniel Whitney
  CanWest Interactive, a division of CanWest MediaWorks Publications Inc.
  1450 Don Mills Rd
  Don Mills ON M3B 2X7
  Tel: (416) 442-2957
  Fax: (416) 442-2968
Email: dwhitney(_at_)canwest(_dot_)com


-----Original Message-----
From: cknell(_at_)onebox(_dot_)com [mailto:cknell(_at_)onebox(_dot_)com]
Sent: Friday, September 14, 2007 2:38 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: Re: [xsl] Repetitive variable definitions

Ooops!

On rexamining the sample input document, I see that the top-most element is 
<example>, so the XPath expression should be:

<xsl:variable name="noofcols"
select="/example/table/tr/td[last()]/@col"/>


--
Charles Knell
cknell(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     Abel Braaksma <abel(_dot_)online(_at_)xs4all(_dot_)nl>
Sent:     Fri, 14 Sep 2007 20:28:41 +0200
To:       xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject:  Re: [xsl] Repetitive variable definitions

In Charles example he assumed that the root of your input document is the table 
element. Maybe you have another root?

to find out whether or not it is somewhere in your doc and you just made

a typo, use the descendants-or-self syntax: (//td)[last()]/@col

it is considered bad coding style, so when you find out that the above 
statement returns something, you should unwind the path again. If you may have 
td elements on last position that do not have an @col attribute, you can remove 
them from the selection path with:
(//td)[(_at_)col][last()]/@col

If you want to know whether or not the node is selected but perhaps contains 
something not visible, use count(....) around the whole thing.
If you get "1" then the @col is empty or contains spaces, otherwise, the

select statement still has a path mismatch.

hth,
-- Abel Braaksma

Whitney, Dan (CanWest Interactive) wrote:
Charles,

Thanks for the fast response, however the noofcols variable is always
empty.

Dan


________________________________

Daniel Whitney
  CanWest Interactive, a division of CanWest MediaWorks Publications
Inc.
  1450 Don Mills Rd
  Don Mills ON M3B 2X7
  Tel: (416) 442-2957
  Fax: (416) 442-2968
Email: dwhitney(_at_)canwest(_dot_)com


-----Original Message-----
From: cknell(_at_)onebox(_dot_)com [mailto:cknell(_at_)onebox(_dot_)com]
Sent: Friday, September 14, 2007 1:36 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Repetitive variable definitions

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

    <xsl:variable name="noofcols"
select="/table/tr/td[last()]/@col"/>

    <xsl:variable name="bg-color">
       <xsl:choose>
          <xsl:when test="$noofcols = 1" select="'red'">
          <xsl:when test="$noofcols = 3 select="'blue'">
          <xsl:otherwise/>
       </xsl:choose>
    <xsl:variable>

    <xsl:template match="table">
        <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">
        <tr bgcolor="{$bg-color}">
           <xsl:apply-templates select="td"/>
        </tr>
    </xsl:template>


    <xsl:template match="td">
        <td bgcolor="{$bg-color}">
        td col variable = <xsl:value-of select="$noofcols"/>
      </td>
    </xsl:template>

</xsl:stylesheet>

--
Charles Knell
cknell(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     Whitney, Dan \(CanWest Interactive\) 
<DWhitney(_at_)canwest(_dot_)com>
Sent:     Fri, 14 Sep 2007 12:24:24 -0500
To:       <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject:  [xsl] Repetitive variable definitions

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>
--~--


--~------------------------------------------------------------------
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>
--~--