Xslt 如何将元素数据的值用作html属性?

Xslt 如何将元素数据的值用作html属性?,xslt,Xslt,在我的xsl文件中,我有以下内容: <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文件中,我有以下内容:

<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>
我试着打印变量的值,结果是正确的。但要将其添加为form元素的action属性值,我得到了一个错误


我甚至尝试过使用这个
action=“{search\uu url}”
和这个:
action=“{@search\uu url}”
,但是我得到的action属性是空的。知道根本原因吗?

问题在于您的变量是有条件创建的。XSLT编译器不喜欢这个,因为它会考虑变量在某些情况下可能不存在。 相反,条件只是变量的值,而不是它的存在

<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>

员工
否则

您正在
$search\uu url
中定义
选择
,它不会在其外部定义。尝试重新排列元素:

<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>

员工
否则
我将
字符串留在
中,但我不确定您是否有意这样做。另外,定义
$search\u url
部分基于
$search\u url
,可能需要使用更具表现力的变量名,这让人非常困惑

<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>