Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL Variable Assignment/Usage

Tags:

xml

xslt

I have an XML file I'm processing with XSL to build a PDF. I'm running into an issue when I try to use an XSL variable. I'm not sure if I'm using it in the wrong scope, assigning it wrong, or calling it wrong. Here's my current code.

<xsl:template name="article-meta-details">
    <xsl:choose>
        <xsl:when test="/article-meta/oldedition = 1"> 
            <xsl:variable name="section_title" select="'Old'"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="section_title" select="'New'"/>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="subtitle">
    <xsl:value-of select="$section_title" />
</xsl:call-template>

This produces the following error:

XPST0008: Variable section_title has not been declared

I tried another solution I found on a different thread which said the variable had to be defined before being used. I thought it was saying to define it with xsl:param but that produced a similar result for me.

XTSE0010: xsl:param must be immediately within a template, function or stylesheet

I tried the code above with the following alterations in both variable places.

<xsl:param name="section_title" select="'Old'"/>

If someone could please point out what I'm doing wrong and/or how to resolve it it'd be very useful for me. Thanks.

Oh, the goal of this code is to replicate this functionality...

<xsl:template name="article-meta-details">
    <xsl:call-template name="subtitle">
    <xsl:with-param name="text" select="'Old'"/>
</xsl:call-template>
like image 284
user3783243 Avatar asked Sep 26 '26 19:09

user3783243


1 Answers

The error message suggests that you are using XSLT 2.0, so you can simply write:

<xsl:template name="article-meta-details">
    <xsl:call-template name="subtitle">
      <xsl:with-param name="text" 
        select="if (/article-meta/oldedition = 1) then 'Old' else 'New'" />
    </xsl:call-template>
</xsl:template>
like image 154
Michael Kay Avatar answered Sep 29 '26 15:09

Michael Kay