Skip to content

SLAXExample

Phil Shafer edited this page Jul 9, 2013 · 2 revisions

Simple example (test-authors-04.slax)

Here is a simple example script along with the XSLT that it translates into.

SLAX

version 1.1;

param $name = "Poe";

var $favorites := {
    <name hidden="yes"> "Spillane";
    <name> "Doyle";
    <name> "Poe";
}

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

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

XSLT

Running this script thru "slaxproc -x" gives the XSLT equivalent:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:slax-ext="http://xmlsoft.org/XSLT/namespace" version="1.0" extension-element-prefixes="slax-ext">
  <xsl:param name="name" select="&quot;Poe&quot;"/>
  <xsl:variable name="favorites-temp-1">
    <name hidden="yes">Spillane</name>
    <name>Doyle</name>
    <name>Poe</name>
  </xsl:variable>
  <xsl:variable xmlns:slax-ext="http://xmlsoft.org/XSLT/namespace" name="favorites" select="slax-ext:node-set($favorites-temp-1)"/>
  <xsl:template match="/">
    <out>
      <!-- Parameters are passed by name -->
      <xsl:call-template name="test">
        <xsl:with-param name="elt" select="&quot;author&quot;"/>
        <xsl:with-param name="name" select="$name"/>
      </xsl:call-template>
    </out>
  </xsl:template>
  <xsl:template name="test">
    <xsl:param name="name"/>
    <xsl:param name="elt" select="&quot;default&quot;"/>
    <xsl:variable name="slax-dot-1" select="."/>
    <xsl:for-each select="$favorites/name">
      <xsl:variable name="this" select="."/>
      <xsl:for-each select="$slax-dot-1">
        <xsl:choose>
          <xsl:when test="$name = $this and not($this/@hidden)">
            <xsl:element name="{$elt}">
              <xsl:copy-of select=".//author[name/last = $this]"/>
            </xsl:element>
          </xsl:when>
          <xsl:when test="$name = $this">
            <xsl:message>
              <xsl:value-of select="concat(&quot;Hidden: &quot;, $name)"/>
            </xsl:message>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

What does this script do ?

The script is a test case (from $srcdir/test/core/) that shows off some many language features.

It looks at a list of authors ($srcdir/test/core/test-authors.xml) and selects the argument ($name) author from the list:

% slaxproc -a name Doyle -g ../tests/core/test-authors-04.slax ../tests/core/test-authors.xml
<?xml version="1.0"?>
<out>
 <author>
   <author>
     <name>
       <first>Arthur Conan</first>
       <last>Doyle</last>
     </name>
     <life-span>
       <born>1859</born>
       <died>1930</died>
     </life-span>
   </author>
 </author>
</out>
Clone this wiki locally