Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble using XSLT variables to hold attributes

Tags:

variables

xslt

In my xslt template I am having a for-each statement. In that for-each I am making different conditions. I want to have there a variable of string type which shall contain class attributes that will be assigned to a <li>.

As I am new to xslt please provide me some examples or how can I achieve what I want to do.

Here is a bit of my code so you can see what I am taking about:

<xsl:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; '0'">
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
        <li><xsl:attribute name="class">
                topNavigLi
                page<xsl:number value="position()" format="1" />
                <xsl:if test="@nodeName='Network' ">
                    has_submenu network
                </xsl:if>
            </xsl:attribute>
            <xsl:if test="@id = $currentPage/@id">
                <xsl:attribute name="class">
                    current topNavigLi
                    page<xsl:number value="position()" format="1" />
                </xsl:attribute>
            </xsl:if>
            <xsl:if test="position() = last()">
                <xsl:attribute name="class">
                    last topNavigLi
                    page<xsl:number value="position()" format="1" />
                </xsl:attribute>
            </xsl:if>

            <xsl:if test="@id = $currentPage/@id and position() = last()">
                <xsl:attribute name="class">
                    current last topNavigLi
                    page<xsl:number value="position()" format="1" />
                </xsl:attribute>
            </xsl:if>

It would be much elegant to have a variable and than concatenate to it while doing tests. I have tried like this but returns nothing.

<xsl:variable name="li_class" select="page"> </xsl:variable>

<xsl:attribute name="class">
    <xsl:value-of select="li_class" /> 
</xsl:attribute>
like image 659
Alexa Adrian Avatar asked Dec 08 '22 22:12

Alexa Adrian


1 Answers

Here is an example doing what I think you are asking for:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <xsl:copy>
       <xsl:apply-templates/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="num">
  <xsl:variable name="vResult">

    <xsl:if test=". mod 2 = 0">Even </xsl:if>
    <xsl:if test=". mod 3 = 0"> mult3</xsl:if>
    <xsl:if test="not(. mod 2 = 0 or . mod 3 = 0)">none</xsl:if>
  </xsl:variable>

  <xsl:variable name="vnormResults" select="normalize-space($vResult)"/>

  <num tests="{$vnormResults}">
   <xsl:value-of select="."/>
  </num>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

the result is:

<nums>
   <num tests="none">01</num>
   <num tests="Even">02</num>
   <num tests="mult3">03</num>
   <num tests="Even">04</num>
   <num tests="none">05</num>
   <num tests="Even mult3">06</num>
   <num tests="none">07</num>
   <num tests="Even">08</num>
   <num tests="mult3">09</num>
   <num tests="Even">10</num>
</nums>
like image 99
Dimitre Novatchev Avatar answered Jan 26 '23 00:01

Dimitre Novatchev