Search

I had read this oldie-but-goodie article a while ago about using xsl to create your own template language, but I'm just getting around to implementing it. Using exslt, this idea is even easier. Here is how to process attribute value templates you've put in your source xml using the evaulate() function.

So source xml element like this:

 <html class='{$current-page}'>
 <!-- insert awesome -->
 </html>

will output as

 <html class='some-page-handle'>
 <!-- insert awesome -->
 </html>

First, be sure to add the appropriate @s in your xsl stylesheet/transform element:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:dyn="http://exslt.org/dynamic" 
     extension-element-prefixes="dyn">

Then you can create a catch-all template match on attributes:

 <xsl:template match ="@*[starts-with(., '{$')]">
      <xsl:param name='value' select="dyn:evaluate(substring-before(substring-after(., '{'), '}'))" />
      <xsl:attribute name='{local-name()}'>
      <xsl:value-of select="$value" />
      </xsl:attribute>
 </xsl:template>

Keep in mind this will be checking all your attributes, so I guess it would be more efficient to add another xpath predicate to specify a namespace or other context.

Create an account or sign in to comment.

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