Drwhite
Drwhite

Reputation: 1695

How to use value-of an element data as an html attribute?

In my xsl file i have this:

<xsl:choose>
<xsl:when test="$search_resource = 'people'">
    <xsl:variable name="search__url"><xsl:value-of select="$search_url"/>employees</xsl:variable>
</xsl:when>
<xsl:otherwise>
    <xsl:variable name="search__url"><xsl:value-of select="$search_url"/></xsl:variable>
    otherwise
</xsl:otherwise>
</xsl:choose>
<form id="searchForm_valeo" action="{$search__url}"></form>

but after viewing the page a got this error:

XSLTApplyError: Internal error: Failed to evaluate the AVT of attribute 'action'.

I have tried to print the value of the variable and it was correct. But to add it as a value of action attribute of form element i got the error

I have even tried to use this action="{search__url}" and this : action="{@search__url}" but i got action atribute empty. any idea of the root cause ?

Upvotes: 0

Views: 204

Answers (2)

Buck Doyle
Buck Doyle

Reputation: 6397

You’re defining $search__url inside the choose, it won’t be defined outside it. Try rearranging your elements:

<xsl:variable name="search__url">
  <xsl:choose>
    <xsl:when test="$search_resource = 'people'"><xsl:value-of select="$search_url"/>employees</xsl:when>
    <xsl:otherwise><xsl:value-of select="$search_url"/>otherwise</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

I left the otherwise string inside the <xsl:otherwise/> but I’m not sure if you intended that. Also, it’s pretty confusing that you’re defining $search__url based in part on $search_url, you might want to use more expressive variable names.

Upvotes: 1

Mitya
Mitya

Reputation: 34576

The problem is your variable is being conditionally created. The XSLT compiler won't like that because it will consider that the variable may, in some circumstances, not exist.

Instead, condition just the variable's value, not its existence.

<xsl:variable name='search__url'>
    <xsl:choose>
        <xsl:when test="$search_resource = 'people'">
            <xsl:value-of select="$search_url"/>employees
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$search_url"/>
            otherwise
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

Upvotes: 1

Related Questions