📄 tag-xsl.xtp
字号:
<s1 title="XSL Tag"><p>An XSL tag can take advantage of Resin's XSL API to transform the bodyof a tag using a stylesheet. Although it's significantly less efficientthan <var/XTP/>, this kind of tag still has useful applications.</p><s2 title="Using the XSL tag"><p>The XSL tag applies the stylesheet to the tag contents. The stylesheetwill convert meaningful content tags into HTML formatting.</p><p>Because the content and the style are separated, it will now be easyto change the stylesheet for a different format.</p><example title="xsl.jsp"><%@ taglib prefix="ct" uri="/WEB-INF/taglib.tld" %><% String author = "John Doe"; String title = "A Guide to XSL";%><ct:xsl stylesheet="tag.xsl"><item> <author><%= author %></author> <title><%= title %></title></item></ct:xsl></example><results>Author: <b>John Doe</b><br>Title: <em>A Guide to XSL</em><br></results><p>The following stylesheet uses <var/StyleScript/>, Resin's readablesyntax for XSL. You can, of course, convert this example to use strictXSL.</p><p>This example needs to explicitly set the <var/output method/> to HTML sothe <var/<br>/> tag will be printed properly. Using normal XML rules,that tag would be printed as <var/<br/>/>.</p><example title="tag.xsl">$output(method=>html);item << Author: $apply-templates(author);<br/> Title: $apply-templates(title);<br/>>>author <<<b>$apply-templates();</b>>>title <<<em>$apply-templates();</em>>></example></s2><s2 title="Implementation of the XSL Tag"><p>The implementation transforms a string to another string. The XSL APIitself can transform many input formats to many output formats, but welljust use the easiest one.</p><p>The stylesheet is compiled to a Java class for performance, and the<var/StylesheetFactory/> is intelligent enough to load an already-compiledclass to avoid overhead.</p><ol><li>Set the stylesheet search path.<li>Create the StyleScript factory.<li>Parse the stylesheet.<li>Create a transformer.<li>Transform the string.</ol><p>One twist that's thrown into this example is the use of <var/MergePath/>.MergePath lets you combine several directories into a single search path andtreat the whole combination as a single Path object. We'll use that doduplicate the search path used by the XTP and XSL-Filter servlets.</p><example title="XslTag.java">package test;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;import com.caucho.transform.*;import com.caucho.vfs.*;import com.caucho.xsl.*;public class XslTag extends BodyTagSupport { private String stylesheet = "default.xsl"; public void setStylesheet(String stylesheet) { this.stylesheet = stylesheet; } public int doEndTag() throws JspException { HttpServletRequest req; req = (HttpServletRequest) pageContext.getRequest(); Path pwd = Vfs.lookup(req.getRealPath("/")); ServletContext app = pageContext.getServletContext(); Path appDir = Vfs.lookup(app.getRealPath("/WEB-INF/xsl")); // search pwd, then appDir, then the classpath/> MergePath mergePath = new MergePath(); mergePath.addMergePath(pwd); mergePath.addMergePath(appDir); mergePath.addClassPath(getClass().getClassLoader()); try { // use StyleScript StylesheetFactory factory = new StyleScript(); // set the search path factory.setStylePath(mergePath); // create the stylesheet Stylesheet xsl = factory.newStylesheet(stylesheet); StringTransformer transformer = xsl.newStringTransformer(); String body = getBodyContent().getString(); // transform the body String value = transformer.transformString(body); JspWriter out = pageContext.getOut(); out.print(value); } catch (Exception e) { throw new JspException(e.toString()); } return SKIP_PAGE; }}</example></s2><s2 title="Summary"><ul><li>Althought <var/XTP/> is more efficient, you can use inlineXSL tranformations.<li><var/StyleScript/> is a readable syntax for XSL.<li><var/MergePath/> creates search paths with the Path API.<li><var/StringTransformer/> transforms strings to strings.</ul></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -