无法在<a> tag. Expression returns empty

时间:2016-07-13 20:37:03

标签: xml xslt

I'm trying to add a condition within a link that looks something like this.

<a class="accordion-toggle" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse" aria-expanded="true" aria-controls="collapse">
  <xsl:value-of select="./Data[@Name='Title']"/>
  <div class=""></div>
  <xsl:attribute name="class">
    <xsl:choose>
      <xsl:when test="/Properties/Data[@ID='ISSOCIAL']/Option[@Selected='true']/Value = 1">
        pull-right down
      </xsl:when>
      <xsl:otherwise>
        pull-right
      </xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
</a>

Unfortunately the class inside the div tag looks like this <div class="">..empty.

However, if I was to just run the following without adding the condition inside the anchor tag it renders fine.

<div class=""></div>
<xsl:attribute name="class">
  <xsl:choose>
    <xsl:when test="/Properties/Data[@ID='ISSOCIAL']/Option[@Selected='true']/Value = 1">
      pull-right down
    </xsl:when>
    <xsl:otherwise>
      pull-right
    </xsl:otherwise>
  </xsl:choose>
</xsl:attribute>

Output looking like this <div class="pull-right down"></div>

Any ideas on why this is happening? any help would be appreciated!

1 个答案:

答案 0 :(得分:1)

如果您要将属性添加到div,则需要将xsl:attribute放在div内。

 <div>
  <xsl:attribute name="class">
    <xsl:choose>
      <xsl:when test="/Properties/Data[@ID='ISSOCIAL']/Option[@Selected='true']/Value = 1">
        pull-right down
      </xsl:when>
      <xsl:otherwise>
        pull-right
      </xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
</div>

由于问题被标记为XSLT 2.0,您只需使用

即可
<div class="{if (/Properties/Data[@ID='ISSOCIAL']/Option[@Selected='true']/Value = 1) then 'pull-right down' else 'pull-right'}">..</div>