Welcome to libslax.org!

JUNOS Scripting Blog

Welcome to libslax, an open-source implementation of the SLAX language.  SLAX is an alternative syntax for XSLT which is tailored for readability and familiarity, following the style of C and Perl.  Programming constructs and XPath expressions are moved from the XML elements and attributes used in XSLT to first class language constructs.  The overabundance of angle brackets and quotes is replaced by the parentheses and curly braces which programmers have enjoyed since the birth of "C".


The strength of SLAX is lies in a few key concepts.  First is the declarative nature of the XSLT language.  Output content and control statements are interwoven in a natural way that makes the generation of XML content simple.  Second the use of XPath expressions and their place as a first-class concept in SLAX.  XPath expressions are used for conditionals in control statement (if/for/for-each) as well as template and function arguments.  Finally, the syntax is familiar and readable.

SLAX allows you to:

  • Use if/then/else instead of <xsl:choose> and <xsl:if> elements
  • Put test expressions in parentheses
  • Use "==" to test equality (to avoid building dangerous habits)
  • Use curly braces to show containment instead of closing tags
  • Perform concatenation using the underscore operator
  • Write text strings using quotes instead of the <xsl:text> element
  • Simplify invoking named templates with a syntax resembling a function call
  • Simplify defining named template with a syntax resembling a function definition
  • Simplify namespace declarations
  • Reduce the clutter in scripts, allowing the important parts to be more obvious to the reader
  • Write more readable scripts

The benefits of SLAX are particularly strong for new developers, since it puts familiar constructs in familiar syntax, allowing them to concentrate in the new topics introduced by XSLT.

Here is a simple example script:

version 1.1;            /* All scripts start with "version" */

param $name = "Poe";    /* Script parameters with default values */

var $favorites := {     /* ":=" avoids RTFs */
   
<name hidden="yes"> "Spillane";
   
<name> "Doyle";     /* Creates elements and attributes */
}

match
/ {               /* A match template */
   
<out> {
       
/* Parameters are passed by name to templates */
        call test
($elt = "author", $name);
   
}
}

template test ($name, $elt = "default") {
   
for $this ($favorites/name) {
       
if ($name == $this && not(@hidden)) {
            element $elt
{
                copy
-of .//author[name/last == $this];
           
}
       
} else if ($name == $this) {
            message
"Hidden: " _ $name;
       
}
   
}
}

The XSLT version of this script is also available, and the "slaxproc" program can be used to easily convert between the two formats.

Complete documentation for the SLAX language is available. libslax includes the "slaxproc" utility, which can be used to run scripts and to convert between XSLT and SLAX.  "slaxproc" also includes a gdb-like debugger and a simple profiler.

Origins of SLAX

SLAX was originally developed as part of the Juniper Networks' JUNOS Operating System.  It is used for on-box scripting to allow users to customize and enhance the CLI.

  • Configuration data can be restricted by adding rules to the configuration checkout process
  • Configuration data can be expanded using macro-like facilities
  • New CLI commands can be added to perform custom diagnosis, display, or task-oriented operations
  • Scripts can be triggered by events within the system, to respond automatically to problems as they occur

Our customers were unhappy with the XSLT syntax and wanted something simpler and more readable.  With SLAX, we were able to give them that, and are now happy to be able to share this as an open-source project.

libslax is written in C, and is built on top of Daniel Veillard's most excellent libxml2 and libxslt libraries.

Links

libslax is hosted by github as https://github.com/Juniper/libslax (releases, docs, wiki, discussions).

libslax is built using the following software projects: libxml2 and libxslt, libcurl, libedit.



Subpages (1): Announcements