L
Loopy
I'm learning XML and XSL at the moment, but I still can't get my head
around the concept of non-updatable variables. I know we can use
recursion to cycle through a data structure and get the sum (say) of a
list of numbers, or a concatination of a set of strings (again,
example), but if I wanted to, for example, keep that value available
for another template later on to use, I can't see how I could do that.
Let me give a better example. I have written and template which is
called "FindMin" that cycles through a tree that finds the minimum. I
have checked the code and, when it it does go through all of the
subtrees and finds the minimum. However, it will only find the minimum
of each of the subtrees, since I cannot keep a global track of the
smallest value, since when I propagate up the tree, I will lose that
value. How could I rectify this?
Here's the code I've written. It may be a bit crude, but that's
because I've been messing around with it. Min and Path are declared as
an empty string (Min) and the root node of the tree in question at
first call.
<xsl:template name="FindMin">
<xslaram name="Min"/>
<xslaram name="Path"/>
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test="@value < $Min">
<!-- Call template with updated min and path -->
<xsl:call-template name="FindMin">
<xsl:with-param name="Min" select="@value"/>
<xsl:with-param name="Path" select="."/>
</xsl:call-template>
</xsl:when>
<xsltherwise>
<!-- Call template with updated path only -->
<xsl:call-template name="FindMin">
<xsl:with-param name="Min" select="$Min"/>
<xsl:with-param name="Path" select="."/>
</xsl:call-template>
</xsltherwise>
</xsl:choose>
</xsl:for-each>
The tree I am using for this test is:
<node value="10">
<node value="15"/>
<node value="5">
<node value="7">
<node value="9"/>
<node value="8"/>
</node>
<node value="11"/>
</node>
</node>
Any ideas any one? I'm sure you've had this question before, but most
answers I've checked seem to use Microsoft's XML variation, which I'm
trying to avoid.
Regards
Johnny Ooi
around the concept of non-updatable variables. I know we can use
recursion to cycle through a data structure and get the sum (say) of a
list of numbers, or a concatination of a set of strings (again,
example), but if I wanted to, for example, keep that value available
for another template later on to use, I can't see how I could do that.
Let me give a better example. I have written and template which is
called "FindMin" that cycles through a tree that finds the minimum. I
have checked the code and, when it it does go through all of the
subtrees and finds the minimum. However, it will only find the minimum
of each of the subtrees, since I cannot keep a global track of the
smallest value, since when I propagate up the tree, I will lose that
value. How could I rectify this?
Here's the code I've written. It may be a bit crude, but that's
because I've been messing around with it. Min and Path are declared as
an empty string (Min) and the root node of the tree in question at
first call.
<xsl:template name="FindMin">
<xslaram name="Min"/>
<xslaram name="Path"/>
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test="@value < $Min">
<!-- Call template with updated min and path -->
<xsl:call-template name="FindMin">
<xsl:with-param name="Min" select="@value"/>
<xsl:with-param name="Path" select="."/>
</xsl:call-template>
</xsl:when>
<xsltherwise>
<!-- Call template with updated path only -->
<xsl:call-template name="FindMin">
<xsl:with-param name="Min" select="$Min"/>
<xsl:with-param name="Path" select="."/>
</xsl:call-template>
</xsltherwise>
</xsl:choose>
</xsl:for-each>
The tree I am using for this test is:
<node value="10">
<node value="15"/>
<node value="5">
<node value="7">
<node value="9"/>
<node value="8"/>
</node>
<node value="11"/>
</node>
</node>
Any ideas any one? I'm sure you've had this question before, but most
answers I've checked seem to use Microsoft's XML variation, which I'm
trying to avoid.
Regards
Johnny Ooi