I was building a Data View in SharePoint Designer that showed a training schedule. I wanted to use a conditional statement to build the XPath expression that selected the classes. If the page was passed a CourseTitle parameter in the querystring then I wanted to use it to select only classes scheduled for that course. If there wasn't a CourseTitle parameter I wanted to select all scheduled classes.
First I tried a choose statement like the following...
<xsl:choose>
<xsl:when test="string-length(CourseTitle) != 0">
<xsl:variable name="Rows" select="/dsQueryResponse/Class_Schedule/Rows/Row[normalize-space(@SC_Course_Title) = $CourseTitle and number(translate(substring before(@Start_x0020_Date,'T'),'-','')) > number(translate(substring-before($Today,'T'),'-',''))]">
</xsl:when>
<xsl:otherwise>
<xsl:variable name="Rows" select="/dsQueryResponse/Class_Schedule/Rows/Row[number(translate(substring-before(@Start_x0020_Date,'T'),'-','')) > number(translate(substring-before($Today,'T'),'-',''))]">
</xsl:otherwise>
</xsl:choose>
I also tried just two separate "<xsl:if statements />" but that didn't work either.
The error message I got was "a required variable (Rows) isn't present". As pointed out by a colleague (Jeremy Luerkens), the error was being caused later in the application when I tried to reference the "Rows" variable. The "Rows" variable was out of scope at that point and no longer valid.
I had to separate the conditional statement into its own variable (condition) then in the "Rows" variable I had to set the node to "self::node" when calling the "condition" variable so the select would get created in the right scope.
<xsl:variable name="condition" select="boolean(string-length($QSCourseTitle) = 0)"/>
<xsl:variable name="Rows" select="self::node()[$condition] /dsQueryResponse/Class_Schedule/Rows/Row[number(translate(substring-before(@Start_x0020_Date,'T'),'-','')) > number(translate(substring-before($Today,'T'),'-',''))] | self::node()[not($condition)] /dsQueryResponse/Class_Schedule/Rows/Row[normalize-space(@SC_Course_Title) = $QSCourseTitle and number(translate(substring-before(@Start_x0020_Date,'T'),'-','')) > number(translate(substring-before($Today,'T'),'-',''))]"/>