Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding html class according to xslt statement

Tags:

xslt

xslt is pretty new for me. Is it possible to do something similar to my code below. I know it is possible in other template languages.

  <div class="<xsl:if test="position()=1">myclass</xsl:if>">Hello</div> 
like image 995
pethel Avatar asked Feb 16 '23 21:02

pethel


2 Answers

You could wrap an xsl:attribute in an xsl:if...

    <div>
        <xsl:if test="position()=1">
            <xsl:attribute name="class">myclass</xsl:attribute>
        </xsl:if>
        <xsl:text>Hello</xsl:text>
    </div>

Also, in XSLT 2.0, you can write the xsl:attribute like this:

<xsl:attribute name="class" select="'myClass'"/>

Another XSLT 2.0 option, if you don't mind having an empty class="", is to use an if in an AVT (Attribute Value Template):

<div class="{if (position()=1) then . else ''}">...</div>

The then may vary depending on context.

like image 183
Daniel Haley Avatar answered Feb 19 '23 10:02

Daniel Haley


It should be something like this:

<xsl:variable name="myclass" select="variablenode" />
    
<div class="adf">
<xsl:if test="yournode[position()=1]">
    <xsl:value-of select="$myclass"/>
</xsl:if>
Hello</div> 

But please give us your source XML, the XSLT you have so far and the expected output. Otherwise we can only guess.

like image 42
Peter Avatar answered Feb 19 '23 10:02

Peter