views:

238

answers:

3

How I can check defined variable $var01 or not? The problem there are:

<input type="text" name="search_do" style="width: 150px;" value="{$search_do}" />

But it isn't work, I have a message "runtime error" if $search_do isn't defined.

P.S. I can't edit php back-end, just XSL template

+5  A: 

When $search_do is not defined, then it has not been declared (in the current scope).

It's a simple as that - there is no "conditional variable definition" in XSLT. They can't be sometimes defined and sometimes not, they are always the one or the other. An "is defined check" is completely unnecessary, that's why there is none. You can always see from your code if a variable is there or not.

Variables are strictly scoped, though. The are valid only in within their parent element. This means you can't do

<xsl:if test="some-condition">
  <xsl:variable name="search_do" value="foo" />
  <!-- search_do goes out of scope right away! -->
</xsl:if>

<!-- $search_do will not be valid here -->
<input type="text" name="search_do" value="{$search_do}" />

but rather

<xsl:if test="some-condition">
  <xsl:variable name="search_do" value="foo" />

  <!-- use it as long as it is in scope -->
  <input type="text" name="search_do" value="{$search_do}" />
</xsl:if>
Tomalak
`$search_do` could be defined in the global scope, even in an imported file.
Kyle Butt
Yeah it could. But there is no conditional import either. You can look at the file(s) and you see if the variable will be there or not. This is nothing that can vary at runtime.
Tomalak
+1  A: 

If $search_do is not defined that means it is no present in the current scope. You cannot test for variables/parameters that are not defined. However, you can test whether tyey are not set and act upon this information.

<xsl:template match="test">
    <xsl:param name="fred"/>
    <result>
        <xsl:if test="$fred">
            <test hasFred="true"></test>
        </xsl:if>

        <xsl:element name="test">
            <xsl:attribute name="hasFred">
                <xsl:choose>
                    <xsl:when test="$fred">
                        <xsl:text>true</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>true</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
        </xsl:element>
    </result>
</xsl:template>
Obalix
`$fred` could be set to an empty node set, or false, or the empty string and you would not be able to tell the difference with `test="$fred"`. Strictly speaking you cannot check if it has been "set", you can only check its value.
Tomalak
Well as far as I undererstood the original question it was not necessary to test for a certain value of `$fred`. If there is a need for this, it could obviously accomplished. And, yes you are right it only applies a logical test similar to using `if (varname) ...` in JavaScript.
Obalix
A: 

What do you really want?

I think rather than using a variable which hasn't been defined, (which is by definition undefined), you want a default value (something like the empty string or the empty node set).

Try something like the following:

<xsl:template match="foo">
  <xsl:param name="search_do"/>
  <input type="text" name="search_do" style="width: 150px;" value="{$search_do}" />
</xsl:template>

...
  <xsl:apply-templates/>
    <!-- ^^ uses the default -->
  <xsl:apply-templates><xsl:with-param name="search_do" select="5"/></xsl:apply-templates>
    <!-- ^^ sets an explicit value for search_do -->
Kyle Butt