Question

I have an xsl which looks something like:

<xsl:param name="relativeURL"/>
<xsl:param name="isPersonalPage" />
<xsl:template match="/">
    <xsl:call-template name="main_level" >
        <xsl:with-param name="urlMatched" select="siteMap/siteMapNode/siteMapNode/@url= $relativeURL" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="main_level" match="/">
<div>
<xsl:param name="urlMatched" />
        <xsl:for-each select="siteMap/siteMapNode/siteMapNode">
                <xsl:choose>
                <xsl:when test="(@url = $relativeURL)">
                    <a class="top_link active">
                        <xsl:attribute name="href">
                            <xsl:value-of select="@url"/>
                        </xsl:attribute>
                            <xsl:value-of select="@topNavTitle"/>
                    </a>
                </xsl:when>
                <xsl:otherwise>
                            <xsl:choose>
                            <xsl:when test="($isPersonalPage = 'true') and (!($urlMatched))">
                                <a class="top_link active">
                                    <xsl:attribute name="href">
                                        <xsl:value-of select="@url"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="@topNavTitle"/>
                                </a>
                            </xsl:when>
                            <xsl:otherwise>
                                <a class="top_link">
                                <xsl:attribute name="href">
                                    <xsl:value-of select="@url"/>
                                </xsl:attribute>    
                                <xsl:value-of select="@topNavTitle"/>           
                                </a>
                            </xsl:otherwise>
                            </xsl:choose>
                </xsl:otherwise>
                </xsl:choose>
        </xsl:for-each>
</xsl:template>

So, basically i need to loop through the node and see if the url attribute of any node matches with a specific URL. If so set value of a variable to something else to something else. Then in the called template "main_nav" I wish to do something based on the value of "urlMatched" variable. But i am not sure I can alter value of a variable in between or not. Can anyone help me with any solution to this problem?

Was it helpful?

Solution 2

This doesn't require a for-each due to the way equals tests work when one side is a node set. Simply

<xsl:variable name="urlMatched"
    select="siteMap/siteMapNode/siteMapNode/@url = $relativeUrl" />

will do what you need, as the expression is true if any of the nodes in the set on the left matches the value on the right, and false otherwise. You should be able to test this value later using <xsl:if test="$urlMatched">.

As for using the value in other templates, remember that variables in XSLT are lexically scoped - you will need to pass a parameter if you want to use the value in another template

<xsl:template name="something">
  <xsl:param name="urlMatched" />
  <!-- template body here -->
</xsl:template>

...
  <xsl:call-template name="something">
    <xsl:with-param name="urlMatched"
    select="siteMap/siteMapNode/siteMapNode/@url = $relativeUrl" />
  </xsl:call-template>

Or just do the calculation in the called template rather than the caller, as call-template doesn't change the context so the same select expression will work there too.

OTHER TIPS

Remember that variables are read-only in XSLT. That is, you can aasign them only once. After that they are read-only.

See this related question

update the variable in xslt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top