the varable thing...

R

R

Hello everybody.

I hava a trouble with variables.

Value of my variable depends on count() if count() returns a number
less then 2
my variable ($width) use default value: 2

here is my XSLT code:

<xsl:choose>
<xsl:when test="count(../group[@style='inline']/*) > 2">
<xsl:variable name="width">
<xsl:value-of select="count(../group[@style='inline']/*)"/>
</xsl:variable>
</xsl:when>
<xsl:eek:therwise>
<xsl:variable name="width">2</xsl:variable>
</xsl:eek:therwise>
</xsl:choose>

and later on:

<tr>
<th colspan="{$width}">....

and when my PHP5 DOM Parser reaches this moment i receive error:

Warning: runtime error: file
/home/xh/workspace/ERS_new/stylesheets/utils/xform.xsl element th in
Warning: unregistered variable width in

if I replace $width with count(...) everything is OK - but I need a
variable
to handle it when for example count() < 2

and by the way - one more question

count(../group[@style='inline']/*) returns number of children of all
group
elements with attribute style.
How can I select ony the maximal count of children of groups elements?

thanks in advance for any help
best regards
R
 
M

Martin Honnen

R wrote:

I hava a trouble with variables.

Value of my variable depends on count() if count() returns a number
less then 2
my variable ($width) use default value: 2

here is my XSLT code:

<xsl:choose>
<xsl:when test="count(../group[@style='inline']/*) > 2">
<xsl:variable name="width">
<xsl:value-of select="count(../group[@style='inline']/*)"/>
</xsl:variable>
</xsl:when>
<xsl:eek:therwise>
<xsl:variable name="width">2</xsl:variable>
</xsl:eek:therwise>
</xsl:choose>

Those variables are then only known in the scope of the parent element,
meaning not in any way "later on".
So you probably need
<xsl:variable name="width">
<xsl:choose>
...
</xsl:choose>
<xsl:eek:therwise>...</xsl:eek:therwise>
</xls:variable>
 
D

David Carlisle

Hello everybody.

I hava a trouble with variables.

The scope of a variable binding is the element in which the binding
appears so her:
<xsl:choose>
<xsl:when test="count(../group[@style='inline']/*) > 2">
<xsl:variable name="width">
<xsl:value-of select="count(../group[@style='inline']/*)"/>
</xsl:variable>
</xsl:when>

The scope is the xsl:when and it's gone out of scope as soon as it's defined.
<xsl:eek:therwise>
<xsl:variable name="width">2</xsl:variable>
</xsl:eek:therwise>
</xsl:choose>

You should make your code match the english description of what you
wanted to do: you didn't want to only conditionally declare a variable
depending on whether some condition was true, you want to
unconditionally declare the variable, with a value that depends on some
condition, so place the condition in the content of the variable binding:

<xsl:variable name="width">
<xsl:choose>
<xsl:when test="count(../group[@style='inline']/*) > 2">
<xsl:value-of select="count(../group[@style='inline']/*)"/>

</xsl:when>
<xsl:eek:therwise>2</xsl:eek:therwise>
</xsl:choose>
</xsl:variable>

Actually for the test you don't need to count them all and then compare
that against 2, you just want to know if there is a third node, so

<xsl:when test="(../group[@style='inline']/*)[3]">

(most uses of count() in a test can be replaced in the same way)

In this case I think your value is in fact 2 + the number of nodes in
the source (not counting the first 2, if they exist) in which case you
can define that directly as


<xsl:variable name="width" select="2 + count((../group[@style='inline']/*)[position()&gt;2])">"/>

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top