if defined in XSLT
How I can check defined variable $开发者_如何学Pythonvar01 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
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>
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>
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 -->
精彩评论