Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

:choose CSS-class

Status
Not open for further replies.

Tiram

Programmer
Apr 11, 2005
11
NO
Hi all!

I have something of a basic question. It seems I was even more out of practice than I thought when I took on this job ...

The XSLT below generates a menu in the form of an Unordered List. I've managed to make it appear where I want it, but I also need to assign a different .CLASS to each LIst item (1 to 8). Can someone help?

Code:
<xsl:template name="MENU">
<!-- Applies the horisontal menu -->
<div id="horimeny">
<div id="meny">
<ul>
<xsl:apply-templates select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/*[PARENT = $INTERNETROOT]" mode="MAIN_MENU"/>
</ul>
</div><!-- END #meny -->
</div><!-- END #horimeny -->
</xsl:template>

<xsl:template match="FOLDERS/*" mode="MENU">
<xsl:variable name="url">
  <xsl:choose>
    <xsl:when test="starts-with(LABEL, '[URL unfurl="true"]http://')"><xsl:value-of[/URL] select="LABEL"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="URL/text()"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="pos" select="count(preceding-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Horisontalmeny'])"/>

<li>
  <xsl:if test="contains($PATH, concat(',', ID, ','))">
    <xsl:attribute name="class">
      <xsl:text>current</xsl:text>
      <xsl:if test="$pos = 0 or LABEL = 'home'"><xsl:text> first</xsl:text></xsl:if>
      <xsl:if test="not(following-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Horisontalmeny'])"><xsl:text> last</xsl:text></xsl:if> 
    </xsl:attribute>   
  </xsl:if>
  <!--<xsl:value-of select="$pos"/>-->
  <a href="{$url}"><span><xsl:apply-templates select="NAME/text()"/></span></a>
</li>

I'd also like to me rid of the submenu here ... I've managed it temporarily by using "display: none" in the CSS, but that's hardly the way to do it!

I hope someone can help me out -- I'd be ever so greatful:)
 
Its not very clear what you want to achieve. Why not use the node position to name the classes?
Code:
<li class="myclass{position()}">
 
I want an unordered list where each list item can be given a different class, something like this:

Code:
<ul>
<li class="red">Item 1</li>
<li class="blue">Item 2</li>
...
<li class="yellow">Item 8</li>
</ul>

I can generate the list fine, but I haven't figured out yet how to assign a different class to each list item. Is that clearer?

And thanks for helping:)
 
Create a variable with nodeset of class names:
Code:
<xsl:variable name="classes">
  <class>red</class>
  <class>blue</class>
  ....
  <class>yellow</class>
</xsl:variable>
Then do:
Code:
<xsl:variable name="pos" select="position()"/>
  <li class="{classes/class[position()=$pos]}">
    ....
Depending on your XSL parser, you may need to use the msxsl:node-set function.
 
Unfortunately I can't your suggestion right now, seeing as the production server is down:\ I'll be back when it is up and running again!
 
As soon as the server was up again, I tried this out, but I couldn't make it work:\ I'm not too sure I implemented it corectly, though ...

I've pasted the complete XSLT sheet below, in case that might help any:

Code:
<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0"> 

<xsl:output method="html" indent="no" encoding="iso-8859-1" doctype-public="-//W3C//DTD HTML 4.01 Strict//EN" doctype-system="[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd"/>[/URL] 

<xsl:variable name="defaultStyleWrapping">false</xsl:variable> 

<xsl:variable name="INTERNETROOT" select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/FOLDER[LABEL='internett']/ID"/> 

<!-- 
========================================================================== 
Start menu templates, be careful when modifying 
========================================================================== 
--> 

<xsl:variable name="CURRENT_FOLDER_ID" select="(/DOCUMENT/BODY/ARTICLES/ACTION/FOLDER_CONTENTS/PARENT/FOLDER/ID|/DOCUMENT/BODY/ARTICLES/ACTION/ARTICLE/FOLDER_ID)[1]"/> 

<xsl:variable name="CURRENT_TOP_FOLDER_ID"> 
  <xsl:value-of select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/FOLDER[contains($PATH, concat(',', ID, ',')) and number(DEPTH) = 1]/ID"/> 
</xsl:variable> 

<xsl:variable name="CURRENT_SECOND_FOLDER_ID"> 
  <xsl:value-of select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/FOLDER[contains($PATH, concat(',', ID, ',')) and number(DEPTH) = 2]/ID"/> 
</xsl:variable> 

<xsl:param name="PATH"> 
  <xsl:call-template name="TRACE_FOLDER_TO_ROOT"> 
    <!-- Specify id below to tell script what folder ID to use as basis for menu location --> 
    <xsl:with-param name="id" select="(/DOCUMENT/BODY/ARTICLES/ACTION/FOLDER_CONTENTS/PARENT/FOLDER/ID|/DOCUMENT/BODY/ARTICLES/ACTION/ARTICLE/FOLDER_ID)[1]"/> 
  </xsl:call-template> 
</xsl:param> 

<xsl:template name="TRACE_FOLDER_TO_ROOT"> 
<!-- Trace current folder ID to top folder --> 
<xsl:param name="id"/> 
<xsl:param name="str"/> 
<xsl:variable name="parent" select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/FOLDER[ID=$id]/PARENT"/> 
<xsl:variable name="newstr" select="concat($str, ',', $id, ',')"/> 
<xsl:choose> 
  <xsl:when test="$parent and /DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/FOLDER[ID=$parent]"> 
  <!-- Recurse --> 
    <xsl:call-template name="TRACE_FOLDER_TO_ROOT"> 
      <xsl:with-param name="id" select="$parent"/> 
      <xsl:with-param name="str" select="$newstr"/> 
    </xsl:call-template> 
  </xsl:when> 
  <xsl:otherwise> 
  <!-- We're at top --> 
    <xsl:value-of select="$newstr"/> 
  </xsl:otherwise> 
</xsl:choose> 
</xsl:template> 

<xsl:template match="FOLDERS/*" mode="MENU"> 
<xsl:variable name="MYPARENT" select="PARENT"/> 
<!-- Is folder or parent hidden? --> 
<xsl:variable name="HIDDEN" select="ATTRIBUTES/ATTRIBUTE[LABEL='hidden']/VALUE = 'true'"/> 
<xsl:if test="not($HIDDEN) and (not(PARENT) or contains($PATH, concat(',', PARENT, ',')))"> 
  <!-- We're either at root or have a parent in path --> 
  <xsl:apply-templates select="." mode="WRITE"/> 
</xsl:if> 
</xsl:template> 

<xsl:template match="FOLDERS/*" mode="LEFTMENU"> 
<xsl:variable name="MYPARENT" select="PARENT"/> 
<!-- Is folder or parent hidden? --> 
<xsl:variable name="HIDDEN" select="ATTRIBUTES/ATTRIBUTE[LABEL='hidden']/VALUE = 'true'"/> 
<xsl:if test="not($HIDDEN) and (not(PARENT) or contains($PATH, concat(',', PARENT, ',')))"> 
  <!-- We're either at root or have a parent in path --> 
  <xsl:apply-templates select="." mode="LEFTWRITE"/> 
</xsl:if> 
</xsl:template> 


<!-- 
========================================================================== 
End menu templates 
========================================================================== 
--> 

<xsl:template name="MENU"> 
<!-- Applies the horisontal menu --> 
<div id="horimeny"> 

<div id="nav"> 
<a href=""><img src="files/nav-eng.gif" alt="In English" width="14" height="14" /></a> &#160; 
<a href="./"><img src="files/nav-hjem.gif" alt="Hjem" width="13" height="14" /></a> &#160; 
<a href="mailto:post@energi.no"><img src="files/nav-kontakt.gif" alt="Kontakt" width="13" height="14" /></a> 
</div><!-- END #nav --> 

<div id="meny"> 
<ul> 
<xsl:apply-templates select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/*[PARENT = $INTERNETROOT]" mode="MAIN_MENU"/> 
</ul> 
</div><!-- END #meny --> 

</div><!-- END #horimeny --> 
</xsl:template> 

<xsl:template match="FOLDERS/*" mode="MENU"> 
<xsl:variable name="url"> 
  <xsl:choose> 
    <xsl:when test="starts-with(LABEL, '[URL unfurl="true"]http://')"><xsl:value-of[/URL] select="LABEL"/></xsl:when> 
    <xsl:otherwise><xsl:value-of select="URL/text()"/></xsl:otherwise> 
  </xsl:choose> 
</xsl:variable> 

<xsl:variable name="pos" select="count(preceding-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Horisontalmeny'])"/> 

<li> 
  <!-- xsl:if test="contains($PATH, concat(',', ID, ','))" --> 
    <xsl:attribute name="class"> 
      <xsl:text>item</xsl:text><xsl:value-of select="$pos"/> 
    </xsl:attribute> 
  <!-- /xsl:if --> 
<!--<xsl:value-of select="$pos"/>--> 
<a href="{$url}"><span><xsl:apply-templates select="NAME/text()"/></span></a> 
</li> 
</xsl:template> 

<xsl:template name="MAIN_MENU"> 
<!-- Applies the main menu --> 
<div id="leftmeny"> 
<ul> 
<xsl:apply-templates select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/*[PARENT = $INTERNETROOT]" mode="MAIN_MENU"/> 
</ul> 
</div> 
</xsl:template> 

<xsl:template match="FOLDERS/*" mode="MAIN_MENU"> 
<xsl:variable name="url"> 
  <xsl:choose> 
    <xsl:when test="starts-with(LABEL, '[URL unfurl="true"]http://')"><xsl:value-of[/URL] select="LABEL"/></xsl:when> 
    <xsl:otherwise><xsl:value-of select="URL/text()"/></xsl:otherwise> 
  </xsl:choose> 
</xsl:variable> 

<xsl:variable name="pos" select="count(preceding-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Venstremeny'])"/> 

<li> 
  <xsl:if test="contains($PATH, concat(',', ID, ','))"> 
    <xsl:attribute name="class"> 
      <xsl:text>current</xsl:text> 
      <xsl:if test="$pos = 0 or LABEL = 'home'"><xsl:text> first</xsl:text></xsl:if> 
      <xsl:if test="not(following-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Venstremeny'])"><xsl:text> last</xsl:text></xsl:if> 
    </xsl:attribute>    
  </xsl:if> 
  <!--<xsl:value-of select="$pos"/>--> 
  <a href="{$url}"><xsl:apply-templates select="NAME/text()"/></a> 

  <xsl:if test="../*[PARENT = current()/ID] and contains($PATH, concat(',', ID, ','))"> 
    <ul> 
      <xsl:apply-templates select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/*[PARENT=current()/ID]" mode="MAIN_MENU"/> 
    </ul> 
  </xsl:if> 
</li> 
</xsl:template> 

<xsl:template match="FOLDERS/*" mode="SUB_MENU"> 
<xsl:variable name="url"> 
  <xsl:choose> 
    <xsl:when test="starts-with(LABEL, '[URL unfurl="true"]http://')"><xsl:value-of[/URL] select="LABEL"/></xsl:when> 
    <xsl:otherwise><xsl:value-of select="URL/text()"/></xsl:otherwise> 
  </xsl:choose> 
</xsl:variable> 

<xsl:variable name="pos" select="count(preceding-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Undermeny'])"/> 

<li> 
  <xsl:if test="contains($PATH, concat(',', ID, ','))"> 
    <xsl:attribute name="class"> 
      <xsl:text>current</xsl:text> 
      <xsl:if test="$pos = 0"><xsl:text> first</xsl:text></xsl:if> 
    </xsl:attribute> 
    
  </xsl:if> 
  <a href="{$url}"><xsl:apply-templates select="NAME/text()"/></a> 

  <xsl:if test="../*[PARENT = current()/ID] and contains($PATH, concat(',', ID, ','))"> 
    <ul> 
      <xsl:apply-templates select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/*[PARENT=current()/ID]" mode="MAIN_MENU"/> 
    </ul> 
  </xsl:if> 
</li> 
</xsl:template> 

<xsl:template match="FOLDERS/*[DEPTH &gt; 2]" mode="MAIN_MENU"> 
<xsl:variable name="url"> 
  <xsl:choose> 
    <xsl:when test="starts-with(LABEL, '[URL unfurl="true"]http://')"><xsl:value-of[/URL] select="LABEL"/></xsl:when> 
    <xsl:otherwise><xsl:value-of select="URL/text()"/></xsl:otherwise> 
  </xsl:choose> 
</xsl:variable> 

<li> 
  <xsl:if test="contains($PATH, concat(',', ID, ','))"> 
    <xsl:attribute name="class"><xsl:text>current</xsl:text></xsl:attribute> 
  </xsl:if> 
  <a href="{$url}"><xsl:apply-templates select="NAME/text()"/></a> 

  <xsl:if test="../*[PARENT = current()/ID] and contains($PATH, concat(',', ID, ','))"> 
    <ul> 
      <xsl:apply-templates select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/*[PARENT=current()/ID]" mode="MAIN_MENU"/> 
    </ul> 
  </xsl:if> 
</li> 
</xsl:template> 

<xsl:template match="FOLDERS/*[ATTRIBUTES/ATTRIBUTE[LABEL = 'hidden']/VALUE = 'true']" mode="QUICK_LINKS"/> 
<xsl:template match="FOLDERS/*[ATTRIBUTES/ATTRIBUTE[LABEL = 'hidden']/VALUE = 'true']" mode="MAIN_MENU"/> 
<xsl:template match="FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'hidden']/VALUE = 'true']" mode="submenuitem"/> 

<xsl:template match="FOLDERS/*[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Skjul']" mode="QUICK_LINKS"/> 
<xsl:template match="FOLDERS/*[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Skjul']" mode="MAIN_MENU"/> 
<xsl:template match="FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Skjul']" mode="submenuitem"/> 

</xsl:stylesheet>
 
All solved:) In case you want to know, this is the bit that did it:

Code:
<xsl:template match="FOLDERS/*" mode="MAIN_MENU">
<xsl:variable name="url">
  <xsl:choose>
    <xsl:when test="starts-with(LABEL, '[URL unfurl="true"]http://')"><xsl:value-of[/URL] select="LABEL"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="URL/text()"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="pos" select="count(preceding-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Venstremeny'])"/>

<li class="">
    <xsl:attribute name="class">
      <xsl:choose> 
        <xsl:when test="not(following-sibling::FOLDER[ATTRIBUTES/ATTRIBUTE[LABEL = 'menu_new']/VALUE = 'Horisontalmeny'])"><xsl:text>item</xsl:text><xsl:value-of select="$pos"/></xsl:when> 
        <xsl:otherwise><xsl:text>item</xsl:text><xsl:value-of select="$pos"/></xsl:otherwise> 
      </xsl:choose>
    </xsl:attribute>
  <a href="{$url}"><xsl:apply-templates select="NAME/text()"/></a>

  <xsl:if test="../*[PARENT = current()/ID] and contains($PATH, concat(',', ID, ','))">
    <ul>
      <xsl:apply-templates select="/DOCUMENT/BODY/ARTICLES/ACTION/FOLDERS/*[PARENT=current()/ID]" mode="MAIN_MENU"/>
    </ul>
  </xsl:if>
</li>
</xsl:template>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top