Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign CSS class to element in an XSL

Tags:

xslt

My XML file is as follows:

<worksheet>
<row>
<rowTitle>RT1</rowTitle>
<rowType>yesorno</rowType>
<subLine>subLine1Content</subLine>
<subLine>subLine2Content</subLine>
</row>
<row>
<rowTitle>RT2</rowTitle>
<rowType>operation</rowType>
<subLine>subLine1Content</subLine>
<subLine>subLine2Content</subLine>
<subLine>subLine3Content</subLine>
</row>
.
.
</worksheet>

in my xsl, while displaying contents of a particular row, i'd like to add a class to the html element that'll specify the type of the row it is. I tried using xsl:choose and assigning value to a xsl:variable, but that doesn't work. I'm trying to display the row as

<ol>
<li class="rowTypeBoolean">
RT1
<ul><li>subLineContent1</li>
<li>subLineContent2</li></ul>
</li>
<li class="rowTypeOptions">
RT2
<ul><li>subLineContent1</li>
<li>subLineContent2</li>
<li>subLineContent3</li></ul>
</li>
.    
.
</ol>

XSL file snippet

<xsl:template match="row">
        <li class="rowClass ${className}">
            <xsl:choose>
                <xsl:when test="type = 'YESORNO'">
                    <xsl:variable name="className" select="rowTypeBoolean"/>
                </xsl:when>
                <xsl:when test="type = 'OPTIONS'">
                    <xsl:variable name="className" select="rowTypeOptions"/>
                </xsl:when>
                <xsl:when test="type = 'OPERATION'">
                     <xsl:variable name="className" select="rowTypeOperation"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="className" select="rowTypeOther"/>
                </xsl:otherwise>
            </xsl:choose>
            <span class="rowTitleClass">
                <xsl:value-of select="rowtitle"/>
            </span>
            <br/>
            <ul class="subLineListClass">
                <xsl:apply-templates select="subLine"/>
            </ul>
        </li>
</xsl:template>
like image 842
charudatta Avatar asked Aug 09 '10 18:08

charudatta


People also ask

How do you assign values in XSLT?

XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

Can we reassign a value to variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.

Is xsl and XSLT the same?

XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.


1 Answers

You need to add it as an attribute to the element:

    <li>
        <xsl:choose>
            <xsl:when test="type = 'YESORNO'">
                <xsl:attribute name="className">rowTypeBoolean</xsl:attribute>
            </xsl:when>
            <xsl:when test="type = 'OPTIONS'">
                <xsl:attribute name="className">rowTypeOptions</xsl:attribute>
            </xsl:when>
            <xsl:when test="type = 'OPERATION'">
                <xsl:attribute name="className">rowTypeOperation"</xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="className">rowTypeOther"</xsl:attribute>
            </xsl:otherwise>
        </xsl:choose>
    </li>
like image 67
Oded Avatar answered Nov 22 '22 11:11

Oded