在条件下使用XSL在变量中设置值

Bac*_*ice 2 xslt xslt-2.0

我有一个基本条件,可以检查变量是否为空,以及是否将变量设置为特定值,例如这样。

<xsl:variable name="PIC" select="avatar"/>

<xsl:choose>
  <xsl:when test="avatar !=''">
    <xsl:variable name="PIC" select="avatar"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="PIC" select="'placeholder.jpg'"/>
  </xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

基本上,将var PIC设置为任何avatar返回值。然后进行测试,以检查它是否不为空并分配给var PIC,如果为空,placeholder.jpg则向var添加一个值PIC

现在由于某种原因,我不断收到以下警告

A variable with no following sibling instructions has no effect

对我在这里做错的任何想法吗?

Tim*_*m C 6

变量在XSLT中是不可变的,并且一旦设置就无法更改。中的变量声明xsl:choose只是在当前块作用域内局部的新声明。(据说它们“阴影”了初始变量)。

您需要做的是...

<xsl:variable name="PIC">
   <xsl:choose>
     <xsl:when test="avatar !=''">
        <xsl:value-of select="avatar"/>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="'placeholder.jpg'"/>
     </xsl:otherwise>
   </xsl:choose>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)