shindhu
shindhu

Reputation: 36

Passing an local varaible from one template to another template in xslt

I am trying to covert an xml to html using xslt. Now I got struck with the use of variables in xslt. Is it possible to pass variables from parent node to some other template other than child node? I have amy xslt code like this `

<xsl:template match="xpath"name="a">
<xsl:variable name="object" select="Hello">
<xsl:call-template="b">
<xsl:with-param name="object" select="$object"/>
</xsl:call-template>
</xsl:variable>
</xsl:template>
<xsl:template match="xpath" name="b">
<xsl:param name="object"/>
</xsl:template>

`

I am getting an following error unexpected xslt element 'param'

Help me to resolve this issue.

Upvotes: 0

Views: 1162

Answers (1)

Aniket V
Aniket V

Reputation: 3247

There are multiple issues with the code that has been shared.

  1. XSL template cannot have both match and name attributes specified. You can either have a match attribute or name attribute. The match attribute can go with the mode attribute if you are looking to match with same element from input XML.
  2. XSL variable object has been declared and a value of Hello assigned to it. However you are also calling a template b from inside the <xsl:variable> which is not correct. XSL variables cannot have select and content both.

You may want to modify the code as below if you are looking for passing a parameter from one template to another.

<!-- template matching with input XML -->
<xsl:template match="xpath">
    <!-- declare variable "object" and assign value as "Hello" -->
    <xsl:variable name="object" select="'Hello'" />

    <!-- call template "b" and pass the value of variable "object" -->
    <xsl:call-template name="b">
        <xsl:with-param name="object" select="$object" />
    </xsl:call-template>
</xsl:template>

<!-- create template "b" using @name attribute -->
<xsl:template name="b">
    <!-- declare parameter -->
    <xsl:param name="object" />

    <!-- print the value of parameter -->
    <xsl:value-of select="$object" />
</xsl:template>

Upvotes: 1

Related Questions