Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1250 posts 3747 karma points admin c-trib
    Jan 07, 2010 @ 21:37
    Dan
    0

    Use XSLT variable in XSLT operator

    Hi,

    I have a number of divs on a page.  Every 6th div needs to have a different CSS class, so I'm trying to use the 'mod' function.  It works when I hard code the value but not when I use an XSLT variable (of data type 'number').

    The following code DOES work:

    <xsl:choose>
    <xsl:when test="(position() mod 6) = 0">
    <xsl:attribute name="class"><xsl:text>alt-class</xsl:text></xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
    <xsl:attribute name="class"><xsl:text>class</xsl:text></xsl:attribute>
    </xsl:otherwise>
    </xsl:choose>

    But this DOESN'T work:

    <xsl:value-of select="$itemsPerLine"/>
    <xsl:choose>
    <xsl:when test="(position() mod $itemsPerLine) = 0">
    <xsl:attribute name="class"><xsl:text>alt-class</xsl:text></xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
    <xsl:attribute name="class"><xsl:text>class</xsl:text></xsl:attribute>
    </xsl:otherwise>
    </xsl:choose>

    Does anyone know how to get the XSLT variable to work?

    Thanks

  • Anders Burla Johansen 2560 posts 8256 karma points
    Jan 07, 2010 @ 21:53
    Anders Burla Johansen
    0

    Try put it like this in the test:

    test="(position() mod number($itemsPerLine)) = 0"

  • Dan 1250 posts 3747 karma points admin c-trib
    Jan 07, 2010 @ 22:44
    Dan
    0

    Thanks Anders, I've tried that and it gives an error "Error parsing XSLT file".

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jan 07, 2010 @ 23:08
    Chriztian Steinmeier
    0

    Hi Dan,

    I'm guessing the error is due to the placement of the <xsl:value-of /> instruction. You can only add attributes to an element that hasn't been closed or had any child-content (elements or text) written to it. Make sure that your class attribute gets generated immediately after opening the <div> element:

    <div class="default-class">
        <xsl:if test="position() mod number($itemsPerLine) = 0">
            <xsl:attribute name="class">alt-class</xsl:attribute>
        </xsl:if>
        <xsl:value-of select="$itemsPerLine" />
    </div>

    (Note, I've used a shortcut - since there can only be one attribute with the name "class", if you redefine it, it'll take the new value.)

    You should be able to see the reason for the error using the XSLT Visualizer...

    /Chriztian 

  • Dan 1250 posts 3747 karma points admin c-trib
    Jan 07, 2010 @ 23:19
    Dan
    0

    Ah, so I didn't need the line at the start:

    <xsl:value-of select="$itemsPerLine"/>

    Works perfectly now, thanks very much!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies