caucho
 XTP Hello, World


XSL is a pattern matching language like Prolog. It looks at XML nodes one by one and checks the stylesheet for matching patterns. XSL uses XPath as the pattern-matching language. Like Prolog, each XSL "rule" has a match pattern and an execution program, the template.

To use XTP, you need an XTP file and an XSL stylesheet. XTP parses the XTP files as HTML and then evaluates the HTML using the stylesheet. Since XTP is a standard XSL processor, the following tutorials will apply to any other XSL engine. The main difference between XTP and an XSL engine like Xalan, is that XTP will automatically compile the page to a JSP page. You can achieve the same effect with Xalan, but it takes a little more work.

The following example has a single template, matching any <hello> tag. When the XSL engine evaluates a <hello> tag it matches the rule in the stylesheet. In this case, it replaces the <hello> with "Hello, World". The <xsl:text> marks a text replacement.

default.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="hello">
  <xsl:text>Hello, World</xsl:text>
</xsl:template>

</xsl:stylesheet>

The following XTP file is first parsed as an HTML file and then processed by the default.xsl stylesheet. As mentioned above, the <hello/> tag is replaced by "Hello, World".

hello.xtp
My First Tag: <hello/>

My First Tag: Hello, World

This above example hides a few details, namely the default XSL rules. XTP's parser automatically inserts the <html> and <body> elements, but as you see, they're not included in the result. If an XML node doesn't match any stylesheet pattern, XSL applies a default rule. The default rule for elements recursively evaluates the element children, but does not copy the element itself. Text nodes are copied. In the above example, the default rules eat the <html> and <body> elements, evaluating the children, but copy the "My First Tag: " text.

StyleScript equivalent

Resin provides an alternate syntax for XSL. XSL does not scan well; it's hard to find templates and figure out what they're doing. In other words, XSL can become a write-only language. StyleScript aims at having the same functionality of XSL, but making it more maintainable.

default.xsl
$template(hello) <<Hello, World>>

Summary

  • XTP needs an XML file (the .xtp) and an XSL stylesheet.
  • default.xsl is the default stylesheet.
  • xsl:template matches an XML tag and replaces it with new XML.
  • The XPath pattern name matches a named element.
  • << ... >> describes the replacement XML in StyleScript.
  • XTP stylesheets provide syntactic sugar to XSL stylesheets, but are completely equivalent.
  • $template(foo) expands to <xsl:template match="foo">.
  • xsl:text returns verbatim text.

Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.