Author:
tonyarnold
Version:
1.0.2
Release Date:
7 Apr 2010
Category:
Output

Description

This utility is useful in combination with Markdown where it’s not possible to set classes and other attributes on your links and images. Basically, it parses your alt attributes on all elements and looks for common string patterns so that this:

class:some-class;; alt:Some great text;; align:left

Becomes:

class="some-class" alt="Some great text" align="left"

Just separate your attributes using “;;” (the space is important). If you still want to include an alternate text attribute (alt="…") or a title (title="…"), just include them in the list (as in the example above).

XSLT

View Raw
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:str="http://exslt.org/strings"
  extension-element-prefixes="str">
  
  <xsl:template mode="html-filter" match="img/@alt | a/@title">
    <xsl:variable name="keys-and-values" select="str:split(., ';; ')"/>
    <xsl:variable name="attribute-local-name" select="local-name()"/>
    
    <xsl:for-each select="$keys-and-values"> 
      <xsl:choose>
        <xsl:when test="contains(., ':')">      
          <xsl:attribute name="{substring-before(., ':')}">
            <xsl:value-of select="substring-after(., ':')"/>
          </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="{$attribute-local-name}">
            <xsl:value-of select="."/>
          </xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template mode="html-filter" match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template mode="html-filter" match="text()">
    <xsl:value-of select="."/>
  </xsl:template>
  
  <xsl:template mode="html-filter" match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates mode="html-filter" select="@*" />
      <xsl:apply-templates mode="html-filter" />
    </xsl:element>
  </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