This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: XSLT problem


Well, I copied your code and added a few things - like <table> - and it does
in fact "work"
But you have to remember that the value of "position()" depends on the
context node at the point of evaluation.  That might not be what you thought
it was.

For example, the xml file I used to test your code was empty, just this:

<root/>

So only one element was ever matched, and so position()  only ever equaled
1. The <td> class attribute did contain that "1" value, though.   What
happens with your xml file, we can't know because we don't what it is at the
moment, nor how the stylesheet will be adjusted to operate on it.

This is a good example of how a little thought in debugging can pay off.
You didn't get the expression you expected.  You could have included the
value of the position in the cell's content, and seen right away that it
wasn't what you thought.  Like this:

<td class="{$ClassName}">
  abc == <xsl:value-of select='position()'/>
 </td>

Not to knock your efforts, but one needs to make sure that things evaluate
as expected.  This means testing them!  I'm always getting lots of surprises
this way.

One more thing, with your code the value of the "class" attribute is
actually

<td class="

    C2

   ">

In other words,  it includes all the whitespace #PCDATA that you put into
those elements.  That doesn't stop the style sheet from styling, but is
annoying.  You might want to write this instead:

   <xsl:when test="position() mod 2 = 0">C1</xsl:when>

See? No whitespace.

Cheers,

Tom P

[Alex Genis]

> What's wrong with my syntax ?
>
> The code :
>
> <TR>
>  <TD class="C1">
>   abc
>  </TD>
>
>  <TD class="C2">
>   xyz
>  </TD>
> </TR>
>
> works absolutely properly
>
> but the code :
>
> <TR>
>  <xsl:variable name="ClassName">
>   <xsl:choose>
>    <xsl:when test="position() mod 2 = 0">
>     C1
>    </xsl:when>
>    <xsl:otherwise>
>     C2
>    </xsl:otherwise>
>   </xsl:choose>
>  </xsl:variable>
>
>  <td class="{$ClassName}">
>   abc
>  </td>
>
>  <td class="{$ClassName}">
>   xyz
>  </td>
> </TR>
>
> does not work correctly (the system does not pick up any of the classes
above)
> even though the code :
>
> <td>
>  <xsl:value-of select="$ClassName"/>
> </td>
>
> shows correct value of the variable (tbl1 or tbl2 respectively) ???
>



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]