Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving local variable from a different template 1

Status
Not open for further replies.

ssilas

IS-IT--Management
Jun 25, 2004
2
US
Any help on the code below would be greately appreciated. I need help accessing a variable that I set in a different template in the same xslt file. I am not 100% sure if this is even possible. If it is, I would need to know the path syntax to access the variable.

Here's the template and code where the variable is set (with extraneous code removed):


<!--SETTING LOCAL VARIABLE IN A TEMPLATE-->
<xsl:template match="navigation">
<xsl:variable name="myvariable">Hello World</xsl:variable>
</xsl:template>

<!--TRYING TO RETRIEVE VARIABLE IN A DIFFERENT TEMPLATE-->
<xsl:template match="channel">
<td><xsl:copy-of select="$myvariable" /></td>
</xsl:template>

I get the following error:

"Could not find variable with the name of myvariable"

Please help!!!

Thank you,

Sean
 
Yep, your variable is out of scope in another template.
What you could do is:

Define your variable outside a template,
<xsl:stylesheet ...>
<xsl:variable name="bla">
<xsl:value-of select="//bla">
</xsl:variable>
<xsl:template match="/">
...
</xsl:template>

or:

use named templates, where you can use params to pass values.
 
jel,

Thanks for the tip. I think don't think I can declare the variable outside of the template, because I have a "when" statement that dictates the value of the variable.

Do you mind giving me some tips on how to do this with named templates, as you suggested? I am pretty sure I understand how to name a template, but how then do I refer to the variable in that named template?

Thanks again...

Sean
 
OK, here goes, a silly example:
XML
Code:
<root>
   <customer>
      <name>John</name>
      <city>Amsterdam</city>
   </customer>
   <customer>
      <name>Jack</name>
      <city>Rotterdam</city>
   </customer>
   <company>
      <name>Qwerty</name>
      <city>Rotterdam</city>
   </company>
</root>

Using named template:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
   <xsl:template match="/">
      <xsl:apply-templates />
   </xsl:template>

   <xsl:template match="customer" />

   <xsl:template match="company">
      <xsl:text>company: </xsl:text>
      <xsl:value-of select="name" />
      <xsl:call-template name="ParseCusumer">
         <xsl:with-param name="city" select="city" />
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="ParseCusumer">
      <xsl:param name="city" />
      <xsl:for-each select="/root/customer">
         <xsl:value-of select="name" />
         <xsl:choose>
            <xsl:when test="city=$city">
               <xsl:text>: same city</xsl:text>
            </xsl:when>
            <xsl:otherwise>
               <xsl:text>: different city</xsl:text>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:for-each>
   </xsl:template>

</xsl:stylesheet>

Defining variable before calling templates:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
   <xsl:variable name="city">
   <!-- just a silly test -->
      <xsl:choose>
         <xsl:when test="count(/root/company)=1">
            <xsl:value-of select="/root/company/city" />
         </xsl:when>
      </xsl:choose>
   </xsl:variable>

   <xsl:template match="/">
      <xsl:for-each select="/root/customer">
         <xsl:value-of select="name" />
         <xsl:choose>
            <xsl:when test="city=$city">
               <xsl:text>: same city as company</xsl:text>
            </xsl:when>
            <xsl:otherwise>
               <xsl:text>: different city</xsl:otherwise></xsl:text>
         </xsl:choose>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top