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>
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!
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With