xsl-list
[Top] [All Lists]

Re: dynamic construction of path based on param

2003-01-22 11:18:22
raven wrote:
I want to dynamically construct a path based on a
param called $course. Here is my current code:

<xsl:if test='contains($course, "510")'>
        <xsl:variable name="path" select="x510" />
</xsl:if>

<xsl:if test='contains($course, "340")'>
        <xsl:variable name="path" select="x340" />
</xsl:if>


<xsl:apply-templates select='syllabus/$path'/>
A number of mistakes: local variable's scope is its parent element, so both of your variables go out of the scope immediately. Second - variables are not macros, <xsl:variable name="path" select="x510" /> means the variable is bound to *result* of evaluation of 'x510' xpath expression. And syllabus/$path is syntax error in XPath.
What you can do (but it looks a litle bit strange) is

<xsl:variable name="path">
        <xsl:if test='contains($course, "510")'>
                <xsl:value-of select="x510" />
        </xsl:if>

        <xsl:if test='contains($course, "340")'>
                <xsl:value-of select="x340" />
        </xsl:if>
</xsl:variable>

<xsl:apply-templates select='syllabus/*[local-name() = $path]'/>

But that looks like a very convolute (and namespace-buggy) version of

        <xsl:if test='contains($course, "510")'>
                <xsl:apply-templates select='syllabus/x510'/>
        </xsl:if>
        <xsl:if test='contains($course, "340")'>
                <xsl:apply-templates select='syllabus/x340'/>
        </xsl:if>

--
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>