What s Next


StyleScript and XSL

As you've noticed, all of the XSL files for XTP use the traditional XSL syntax in order to grab the new tags added to the XTP file. Traditional XSL is very strict and follows all of the XML guidelines. You can learn about XSL in several books, including XSL Essentials by Michael Fitzgerald.

Resin has taken the liberty to lighten up the XSL syntax specifically for use with XTP files. XSLT-Lite or StyleScript doesn't follow the traditional XSL rules but vastly simplifies the verbosity involved. In this section, we cover a few examples to show how StyleScript relates to XSL.

First, consider this XSL declaration to match on a tag like <centerbig>:

 <xsl:template match="centerbig">   <CENTER><H1>     <xsl:apply-templates select="node()|@*"/>   </H1></CENTER> </xsl:template> 

If we convert this to StyleScript, it would look like this:

 centerbig << <CENTER><H1>    <xsl:apply-templates/> </H1><CENTER> >> 

The matching string value typically found in the match="" of the <xsl:template> is used as a major heading in StyleScript using the << and >> symbols as beginning and end designators. StyleScript is easy to read because the template pattern has its own line. You can rewrite the XSL used to copy input to output using StyleScript:

 node() |@* << <xsl:copy>   <xsl:apply-templates select='node()|@*'/> </xsl:copy> >> 

As you can see, some <xsl:> tags are used in StyleScript. The following list provides examples of the major StyleScript pieces available with Resin.

  • $(expression)—Displays the value of an expression. The XSL equivalent is <xsl:value-of select="expression"/>.

    XSL example:

        <xsl:template match='count[@id]'>         <xsl:value-of select='@id'/>    </xsl:template> 

    StyleScript example:

        count[@id] <<    $(@id)    >> 

  • $apply-imports —Calls an overridden template. The XSL equivalent is <xsl:apply-imports/>. For example, when a style sheet imports another style sheet with a match of the same pattern, the code importee will override the imported match. $apply-imports can be used to "call" the imported match.

  • $apply-templates—Applies template rules to the current element or the element's child nodes. The XSL equivalent is <xsl:apply-templates/>.

    XSL example:

        <xsl:template match="/">      <html>     <body>    <h2>CD Collection</h2>     <xsl:apply-templates/>    </body>     </html>    </xsl:template>    <xsl:template match="cd">    <p>      new CD     <xsl:apply-templates select="title"/>      <xsl:apply-templates select="artist"/>    </P>    </xsl:template> 

    StyleScript example:

        / <<     <html>    <body>    <h2>CD Collection</h2>     $apply-templates     </body>    </html>    >>    cd <<      new CD    >> 

  • $apply-templates(select)—Applies template rules to the current element or the element's child nodes based on a specific match pattern. The XSL equivalent is <xsl:apply-templates select=""/>.

    XSL example:

        <xsl:template match="/">     <html>     <body>     <h2>CD Collection</h2>      <xsl:apply-templates/>     </body>     </html>    </xsl:template>    <xsl:template match="cd">    <p>     new CD     <xsl:apply-templates select="title"/>     <xsl:apply-templates select="artist"/>    </p>    </xsl:template> 

    StyleScript example:

        / <<     <html>     <body>     <h2>CD Collection</h2>     $apply-templates     </body>     </html>    >>    cd <<     new CD    $apply-templates(title);    $apply-templates(artist);    >> 

  • $attribute(name)—Adds an attribute to the current element with 'name'. The XSL equivalent is <xsl:attribute name=""/>.

    XSL example:

        <xsl:template match="cd[@id]">    <cd>      <xsl:attribute name="artist">      <xsl:value-of select="artist" />     </xsl:attribute>    </cd>    </xsl:template> 

    StyleScript example:

        $template(cd{@id}) <<    <cd>     $attribute (artist) <<      $value-of(artist);     >>     </cd>    >> 

  • $attribute-set(name)—Defines an attribute set called 'name'. The XSL equivalent is <xsl:attribute-set name="">.

    XSL example:

        <xsl:attribute-set name="font">     <xsl:attribute name="color">red</xsl:attribute>     <xsl:attribute name="weight">bold</xsl:attribute>    </xsl:attribute-set> 

    StyleScript example:

        $attribute-set(font) <<     $attribute(font-color) <<red>>    $attribute(font-weight) <<bold>>    >>    $template(lp) <<     <cd xsl:use-attribute-sets='font'/>    >> 

  • $call-template(name)—Calls template, 'name', and the passing current node. The XSL equivalent is <xsl:call-template name=""/>.

    XSL example:

        <xsl:template match="car">      <xsl:call-template name="size"/>    </xsl:template> 

    StyleScript example:

        $template(car) <<    $call-template(size)    >> 

  • $choose(test) —Implements a select block where test evaluates to true but is optional. The XSL equivalent is <xsl:choose>.

    XSL example:

        <xsl:choose>     <xsl:when test="price&lt; '25 ' ">     <td bgcolor="#ff0000">     <xsl:value-of select="title"/>     </td>    </xsl:when>     <xsl:otherwise>      <td><xsl:value-of select="title"/></td>      </xsl:otherwise>    </xsl:choose> 

    StyleScript example:

        $template(a) <<     $choose() <<       $when(price<25) <<           <td bgcolor="#ff0000">          $value-of(title)          <td>         >>         $otherwise() <<           <td>$value-of(title)</td>         >>      >>    >> 

  • $comment() << ..... >>—Adds a new comment. The XSL equivalent is <xsl:comment></xsl:comment>.

    XSL example:

        <xsl:comment>This is a comment</xsl:comment> 

    StyleScript example:

        $template(a) <<     $comment() << This is a comment. >>    >> 

  • $copy() << .... >>—Copies the current node only to output. The XSL equivalent is <xsl:copy>.

    XSL example:

        <xsl:copy>    <xsl:apply-templates/>     </xsl:copy> 

    StyleScript example:

        $template(@*|node()) <<    $copy() <<      $apply-templates(@* |node());    >>    >> 

  • $copy-of(select)—Copies the 'select' subtree to output. The XSL equivalent is <xsl:copy-of select=""/>.

    XSL example:

        <xsl:variable name="header">     <tr>     <th>CD</th>     <th>Title</th>      </tr>    </xsl:variable>    <xsl:copy-of select="$header" /> 

    StyleScript example:

        Svariable(header) <<    <tr>     <th>CD</th>     <th>Title</th>     </tr>    >>    $copy-of(header) 

  • $declaration—Adds declaration code.

    StyleScript example:

        $declaration <<    double area(double r)    {     return 3.14 * r * r;    }    >> 

  • $directive.page(attributes)—Sets the current pages directives using 'attributes'. The attributes available are:

    • language—Determines the script language; the default is Java.

    • session—Determines whether to use session; the default is false.

    • errorPage—Specifies the page you want to display if an error occurs.

    • import— Specifies the Java imports you want to use on the page.

    • contentType—Specifies the defined content-type.

    StyleScript example:

        $directive.page(language=Javascript, session=true) 

  • $element(name)—Adds a new element 'name'. The XSL equivalent is <xsl:element name="">.

    XSL example:

        <xsl:element name="performer">    <xsl:value-of select="artist" />    </xsl:element> 

    StyleScript example:

        $template(cd) <<      $element(performer)        $value-of(artist)    >> 

  • $expr(expression)—Prints the value of Java 'expression'.

    StyleScript example:

        $template(cd) <<      $text() <<CD -  >>     $scriptlet() <<      String cd = "CD";    >>     $expr(cd);    >> 

  • $for-each(select) << .... >>—Loops through child nodes based on the 'select' pattern. The XSL equivalent is <xsl:for-each select="">.

    XSL example:

        <xsl:for-each select="cd">    <tr>      <td><xsl:value-of select="title"/></td>      <td><xsl:value-of select="artist"/></td>    </tr>    </xsl:for-each> 

    StyleScript example:

        $template(contents) <<     <ol>    $for-each(cd) <<     <li>$value-of(@title);</li>     <li>$value-of(@artist);</li>    >>     </ol>    >> 

  • $if(test) << .... >>—Executes the template if condition 'test' is true. The XSL equivalent is <xsl:if test="">.

    XSL example:

        <xsl:for-each select="cd">      <xsl:if test="price&lt; '20'">             <tr>            <td><xsl:value-of select="title"/></td>             <td><xsl:value-of select="artist"/></td>           </tr>     </xsl:if>    </xsl:for-each> 

    StyleScript example:

        $template(cds) <<     $for-each(@cd) <<     $if(@price < 20) <<     <tr>            <td>$value-of(@title)</td>            <td>$value-of(@artist)</td>     </tr>     >>    $else if (@price > 20) <<     <td>Too Much</td>      <td></td>     $else <<     <td>N/A</td>      <td></td>     >>      >>    >> 

  • $import(href)—Imports the 'href style sheet. The XSL equivalent is <xsl:import href=""/>.

    XSL example:

        <xsl:import href="cds.xsl"/> 

    StyleScript example:

     $import(cds.xsl) 

  • $output(attributes)—Outputs control. The available attributes are:

    • method—Determines the output type of the page; options include xml, html, and text.

    • version—Specifies the XML version you want to use.

    • encoding—Specifies the character set for final output.

    • omit-xml-declaration—Specifies whether you want to use XML or HTML declarations; set to true or false.

    • indent—If set to true, then pretty-print the output.

    • media-type—Specifies the mime-type for the output.

    • disable-output-escaping—If set to true, then special characters like < are printed as < instead of &lt.

    StyleScript example:

        $output(Method=xml, disable-output-escaping=true) 

  • $param(name) —Declares a new XSL parameter called 'name'. The XSL equivalent is <xsl:param name="">.

    XSL example:

        <xsl:template name="test">     <xsl:param name="title" select="double"/>     <xsl:value-of select="title"/>    </xsl:template>    <xsl:template match="a">    <xsl:call-template name="test">     <xsl:with-param name="title" select="new"/>     </xsl:call-template>    </xsl:template> 

    StyleScript example:

        $template(name=>test) <<    $param(title, select=>double);    $value-of($title);    >>    $template(a) <<    $call-template(test) <<    $with-param(title, select=>new);    >>    >> 

  • $processing-instructions(name)—Adds a processing instruction called 'name. The XSL equivalent is <xsl:processing-instruction name="">.

    XSL example:

        xsl:processing-instruction name="xml-stylesheet">     href="style.css" type="text/css"    </xsl:processing-instruction> 

    StyleScript example:

        $processing-instruction(xml-stylesheet) <<     href="style.css" type="text/css"    >> 

  • $scriptlet() << statements >>—Adds an attribute to the current element with 'name.

    StyleScript example:

        $template(cdsearch)    << $scriptlet() <<     int count = Integer.parseInt(node.getAttribute("cds"));     for (int i = 0; i < count; i++)     out.print(title[i]);     >> >> 

  • $sort(attributes) << .... >> —Sorts node from $apply-templates or $for-each. The XSL equivalent is <xsl:sort select="" />. The attributes are:

    • select—Specifies the value you want to sort.

    • order—The default is ascending.

    • data-type—The default is text; values are text or number.

    XSL example:

        <xsl:for-each select="cd">    <xsl:sort select="artist"/>    <tr>    <td><xsl:value-of select="title"/></td>     <td><xsl:value-of select="artist"/></td>     </tr>    </xsl:for-each> 

    StyleScript example:

        $for-each(cd) <<    $sort(artist)    <tr>     <td>$value-of(title)</td>     <td>$value-of(artist)</td>    </tr>    >> 

  • $template(match) << .... >>—Creates new patterns and replacements. The XSL equivalent is <xsl:apply-templates>. The available attributes are:

    • match—Specifies the required match pattern.

    • name—Specifies the name for possible calls by another template.

    • priority—Specifies an integer determining priority over other templates:

    XSL example:

        <xsl:template match="/">    <html>    <body>    <h1>CD Collection</h1>     <xsl:apply-templates/>     </body>     </html>    </xsl:template> 

    StyleScript example:

        $template(/) <<      <html>    <body>    <h1>CD Collection</h1>     $apply-templates(/)     </body>     </html>    >> 

  • $text() << .... >>—Writes contents of the block to the output. The XSL equivalent is <xsl:text> </xsl:text>.

    XSL example:

        <xsl:text>, and </xsl:text> 

    StyleScript example:

        $text() <<    , and    >> 

  • $value-of(select)—Writes 'select' to the output. The XSL equivalent is <xsl:value-of select="">. For example:

    XSL example:

        <td>    <xsl:value-of select="title"/>    </td> 

    StyleScript example:

        <td>    $value-of(title);    </td> 

  • $variable(name)—Assigns value to 'name' XSL variable. The XSL equivalent is <xsl:variable name="" select="" />.

    XSL example:

        <xsl:variable name="color" select='"red"' /> 

    StyleScript:

        $variable(color, select=>red); 




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net