Etherplain
Etherplain

Reputation: 33

XSLT Attribute string test

I'm editing some existing XSLT so I can change the display of the content to appear in tabs. I've used one of the string variables to assign a div id for individual styling. I'm now trying to test that new id attribute for the name of the first tab and then setting the style to display:block for that one tab. I know the WHEN condition is processing because a style is being applied to the div, but it's all display:none.

I am not particularly good at XSLT (steep learning curve) but I got all but this latter bit to work and I figure it's just because I don't know the proper syntax. Here's the block I'm working with. Showing the whole div block but it's the first dozen lines that matter:

  <div class="container">
<xsl:attribute name="id">
    <xsl:value-of select="substring($tmpTitle, 1, 5)"/>
</xsl:attribute>
<xsl:choose>
        <xsl:when test="@id='First'">
             <xsl:attribute name="style">display:block</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:attribute name="style">display:none</xsl:attribute>
        </xsl:otherwise>
</xsl:choose>

<li>

    <h2>
        <xsl:value-of select="$tmpTitle"/>
    </h2>

    <xsl:if test="$listType != ''">
        <a class="guidelinesLink">
            <xsl:attribute name="href">
                <xsl:apply-templates select="link"/>
            </xsl:attribute>
            <xsl:value-of select="link/url-text"/>
        </a>
    </xsl:if>


    <ul class="itemList">
        <xsl:apply-templates select="//item[../title = $tmpTitle or ../title-ddl = $tmpTitle]">
            <xsl:sort select="title"/>
        </xsl:apply-templates>
    </ul>

</li>
</div>

Upvotes: 2

Views: 534

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66723

The XML that you are transforming does not have an @id who's value is equal to First, so the test="@id='First'" will always be false, and will fall down to the xsl:otherwise.

Instead, bind the value that you are using to create the @id to a variable and use that variable both to create the @id attribute and to determine what value to assign to the @style.

Also, if you are always going to be creating the @style attribute, then you can move the xsl:choose inside the xsl:attribute and only declare it once:

<div class="container">
  <xsl:variable name="identifier" select="substring($tmpTitle, 1, 5)"/>
  <xsl:attribute name="id">
    <xsl:value-of select="$identifier"/>
  </xsl:attribute>
  <xsl:attribute name="style">
    <xsl:text>display:</xsl:text>
    <xsl:choose>
      <xsl:when test="$identifier='First'">
        <xsl:text>block</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>none</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>

Upvotes: 1

Related Questions