Author:
wjnielsen
Version:
1.0
Release Date:
10 Sep 2010
Category:
String Operations

Description

We are dependent on Blueprint CSS and love it. One of the drawbacks in using a CSS framework is that it ties more of the design to your code which can make refactoring your xsl a little challenging.

This utility allows you to submit a string that you’re using for the class attribute of a tag and mathematically adjust (+,-,x,÷) the integer in the span-# portion of the class.

I would think this could be easily modified to be used with the Grid960 system, though I’m not very familiar with that CSS Framework.

XSLT

View Raw
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!--
	Name: Blueprint CSS: Adjust-Span Utility;
	Version: 1.0;
	Author: Will Nielsen;
	Date: 2010-09-10;
	Description: Adjusting span-# mathematically in a string used for the class attribute of a tag
	Dependency:http://blueprintcss.org/

	All source code included in this file is, unless otherwise specified,
	released under the MIT licence as follows:

	***** begin license block *****

	Copyright 2010 Will Nielsen

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.

	***** end license block *****

-->

<xsl:template name="adjust-class-span">
    <xsl:param name="operator" select="'-'"/>
    <xsl:param name="span-int" select="1"/>
    <xsl:param name="class"/>

    <xsl:variable name="part-1" select="substring-before(normalize-space($class), 'span-')"/>
    <xsl:variable name="part-2">
        <xsl:choose>
            <!-- When there are more characters after the span-x bit -->
            <xsl:when test="string-length(substring-after(substring-after(normalize-space($class), 'span-'), ' ')) &gt; 0">
                <xsl:value-of select="substring-after(substring-after(normalize-space($class), 'span-'), ' ')"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="span-val">
        <xsl:choose>
            <!-- When there are more characters after the span-x bit -->
            <xsl:when test="string-length(substring-after(substring-after(normalize-space($class), 'span-'), ' ')) &gt; 0">
                    <xsl:value-of select="substring-before(substring-after(normalize-space($class), 'span-'), ' ')"/>
            </xsl:when>
            <!-- otherwise part-2 is the span value -->
            <xsl:otherwise>
                <xsl:value-of select="substring-after(normalize-space($class), 'span-')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    

    <xsl:variable name="new-span-val">
        <xsl:choose>
            <!-- Subtraction -->
            <xsl:when test="$operator = '-'">
                <xsl:value-of
                    select="number($span-val - $span-int)"
                />
            </xsl:when>

            <!-- Addition -->
            <xsl:when test="$operator = '+'">
                <xsl:value-of
                    select="number($span-val + $span-int)"
                />
            </xsl:when>

            <!-- Multiplication -->
            <xsl:when test="$operator = '*' or $operator = 'x'">
                <xsl:value-of
                    select="number($span-val * $span-int)"
                />
            </xsl:when>

            <!-- Division -->
            <xsl:when test="$operator = '/'">
                <xsl:value-of
                    select="number($span-val div $span-int)"
                />
            </xsl:when>
        </xsl:choose>
    </xsl:variable>


    <!-- RETURN STRING -->
    <xsl:value-of
        select="normalize-space(concat(normalize-space($part-1), ' span-', normalize-space($new-span-val), ' ', normalize-space($part-2)))"/>

</xsl:template>

</xsl:stylesheet>

Discuss this XSLT Utility

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details