tags:

views:

38

answers:

2

The generated HTML page contains reference links that sometimes doesn't exist in my page. I should display this link as a simple label. Currently this is done:

<xsl:choose>
 <xsl:when test="$nb_action != 0">
  <a href="#action">Action (offensive, defensive, reconnaissance)</a>
 </xsl:when>
 <xsl:otherwise>
  Action (offensive, defensive, reconnaissance)
 </xsl:otherwise>
</xsl:choose>

I'm wondering how to simplify my code, how to deactivate node <a></a> ?

My first idea was to delegate a special css class :

<a href="#action">
 <xsl:if test="$nb_action = 0">
  <xsl:attribute name="class">inactive</xsl:attribute>
 </xsl:if>Action (offensive, defensive, reconnaissance)
</a>

But it remains a link...

A workaround that follows:

<a><xsl:if test="$nb_action != 0">
  <xsl:attribute name="href">#action</xsl:attribute>
 </xsl:if>Action (offensive, defensive, reconnaissance)</a>

It is correct to write an html <a> tag without href ?

A: 

What don't you store the string Action(..) into a variable and it in code?

codymanix
I suspected this proposal, but as it is possible to modify an attribute, I thought there was a possibility to modify the item ...
chepseskaf
+2  A: 

You could make that Action text as a variable. This variable still appears in 3 places though.

<xsl:variable name="textval">
Action (offensive, defensive, reconnaissance)</xsl:variable>
<xsl:choose>
 <xsl:when test="0">
  <a href="#action"><xsl:copy-of select="$textval"/></a>
 </xsl:when>
 <xsl:otherwise>
  <xsl:copy-of select="$textval"/>
 </xsl:otherwise>
</xsl:choose>

Edit: Alternatively, if you don't mind an extra, useless span tag, you could use

  <xsl:variable name="condition" select="---condition-here---"/>

  <xsl:variable name="tagname">
   <xsl:choose>
    <xsl:when test="$condition">a</xsl:when>
    <xsl:otherwise>span</xsl:otherwise>
   </xsl:choose>
  </xsl:variable>

  <xsl:element name="{$tagname}">
   <xsl:if test="$condition">
    <xsl:attribute name="href">#action</xsl:attribute>
   </xsl:if>
   Action (offensive, defensive, reconnaissance)
  </xsl:element>

(In Firefox, if we set $tagname to an empty string, then the element won't be applied at all. But the processor could also raise an error, so don't rely on it.)

KennyTM
I like your solution, thanks ;)
chepseskaf
@KennyTM: A minor issue: in order to declare $textval as string data type, use `select="'Action (offensive, defensive, reconnaissance)'"`. Otherwise it would be defined as a Result Tree Fragment.
Alejandro
@Alejandro: Actually I believe the tree fragment is what OP wants, and I should use `copy-of` instead of `value-of`. Updated.
KennyTM