0

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?

Ross Cooper
  • 245
  • 2
  • 10
  • 20

2 Answers2

1

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

Community
  • 1
  • 1
Luixv
  • 8,590
  • 21
  • 84
  • 121
  • What is the alternative. I couldn't understand a single thing in the link you gave. I don't think the scenario there is anything similar to mine. – Ross Cooper Mar 25 '13 at 10:02
  • You cannot update a variable in a loop. You can assign a variable only once. Afterwards is Read-Only. If you try update it you will get a run time error. – Luixv Mar 25 '13 at 10:22
0

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.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • What will my variable contain if it finds or doesn't find the relativeUrl? Cos I need to check the value later and do some coding based on that as well. – Ross Cooper Mar 25 '13 at 09:39
  • @RossCooper I've added some more info that should help you. – Ian Roberts Mar 25 '13 at 09:55
  • Hi Ian, I have updated the question with the actual code that I am using now. I am still not getting the required output. Instead I am getting a 500 error. :( – Ross Cooper Mar 25 '13 at 10:21
  • You were right with the second alternative. I created the variable inside the called template the way you suggested and simply used it. Working perfectly fine. Thanks Ian. :) – Ross Cooper Mar 25 '13 at 11:18