📄 atp.xtp
字号:
<title css='default.css'>Active Template Pages</title><summarylist/><p>ATP (Active Template Pages) is based on XSL stylesheets. Thedocumentation for the specific tags is in the <a href='xsl.html'>XSLstylesheet</a> section. This section describes how ATP works.<p>ATP lets web designers create active pageswithout changing the original text. It uses a separate <ahref='xsl.html'>XSL</a> stylesheet to transform the original documentinto a fancy formatted document. Because the active stylesheet isseparate from the passive content, ATP gives designers a tighterfocus. When worrying about style, designers can concentrate on thestylesheet. When concentrating on content, designers can focus on thetext.<p>ATP makes the input file simpler: it can be plain old HTML. Itseparates the content (*.atp) from the style (*.xsl). The tradeoffis that XSL stylesheets are slightly more complicated than JSP activepages. For JSP, scripts execute exactly where they're placed.ATP has to match HTML to script fragments using patterns.<p>ATP works by matching stylesheet patterns to the input HTML,creating the result HTML following the pattern actions.ATP analyzing the input HTML into a structured HTML treeusing the <a href='xmllib.html'>XML document object model</a>. Foreach node, it finds the best pattern in the XSL and applies theaction. The action prints to the output HTML.<section name=blank title='Blank Stylesheet Example'>In this example, we're using a blank stylesheet. Even with a blankstylesheet, <resin/> does something useful: it prints out all text,removing the tags.<example title='hello.atp'><TITLE>Hello, world</TITLE><H1>Hi, World!</H1><P>The hello, world example is simple.</example><p><resin/> first reads in the ATP file, parsing it like an HTMLfile. It adds optional tags, like <html> and </p> andforces all HTML tags to be lower case.<example title='hello$9342.dom'><html> <head> <title>Hello, world</title> </head> <body> <h1>Hi, World!</h1> <p>The hello, world example is simple.</p> </body></html></example><p>Next, <resin/> starts its matching process at the top. Since the stylesheetis empty, it uses the default rules. The default rules say: processan element's children and print a text node's data.<box class='green'><ol><li>#document, process children <ol> <li><html>, process children <ol> <li><head>, process children <ol> <li><title>, process children <ol> <li>"Hello, world", print to output </ol> </ol> <li><body>, process children <ol> <li><h1>, process children <ol> <li>"Hi, World!", print to output </ol> <li><p>, process children <ol> <li>"The hello, ...", print to output </ol> </ol> </ol> </ol></ol></box><example title='hello$9342.html'>Hello, worldHi, World!The hello, world example is simple.</example></section><section name=page title='Simple Page Template'><resin/>'s ATP can create standard page layout: common backgrounds,navigation, headers and footers. This is a common use for any of theactive content creation tools.<p>This example adds two things to the default stylesheet. Allelements are copied instead of ignored, and the body of the HTML getsa background and a margin.<p>Copying elements is easy. The copy template matches all elements<code/match='*'/>. When <resin/> processes a node whose patternmatches nothing else, it will execute the copy action. The actioncopies the element (<code/xsl:copy-element/>) and processes thechildren (<code/xsl:apply-templates/>).<example><xsl:template match='*'> <xsl:copy-element> <xsl:apply-templates/> </xsl:copy-element></xsl:template></example><p>For the page template pattern, we use <code/match='/html/body'/>.<resin/> will execute the template in place of the body. We couldhave used <code/match='body'/> and for most ATP pages that would workfine. But if someone created an internal <body> tag, say in themiddle of a table, that body tag would get the top level decoration.Probably not what was intended.<example><xsl:template match='/html/body'><eg-em> <!-- cyan background --> <body bgcolor='cyan'> <table width='100%'> <!-- left margin --> <tr><td width='240'></td> <!-- center column --> <td width='80%'> <!-- insert body contents --></eg-em> <xsl:apply-templates/><eg-em> <!-- copyright footer --> <hr> Copyright &copy; 1999 Caucho Technology </td></tr> </table> </body></eg-em></xsl:template></example><p>The translation follows the same order as in the blank stylesheetexample. The body rule is used for the body and the copy-element ruleis used for every other tag.<example><example><TITLE>Hello, world</TITLE><H1>Hi, World!</H1><P>The hello, world example is simple.</example><results><html> <head> <title>Hello, world</title> </head> <body bgcolor='cyan'> <table width='100%'> <tr><td width='240'></td> <td width='80%'> <h1>Hi, World!</h1> <p>The hello, world example is simple. </p> <hr> Copyright &copy; 1999 Caucho Technology </td></tr> </table> </body></html></results></example></section><section name=counter title='Creating a Custom Tag'>All by itself, the template example is cool. But here's somethingmore interesting, creating a custom tag. In this case, we'll justcreate a simple counter.<p>To use the counter tag, just add it to the ATP file.<example title='counter.atp'><example><eg-em>A counter example:</eg-em><counter/></example><results>A counter example: 2</results></example><p>Here's the addition to the stylesheet file.<example title='default.xsl'> <xsl:template match='counter'> <#= application.attribute.counter++ #> </xsl:template></example><p>The <code/xsl:template/> tag says we're defining a new tag. The<code/match='counter'/> tells <resin/> to apply the counter tagwhenever it sees <code/<counter/>/>.<p>JavaScript code is between the special expression tags '<#=' and'#>'. <resin/> will insert the value of the expression into thegenerated text.<p>The <a href='jspapp.html'>application</a> object is the same as forJSP. In fact, stylesheets can use any of the JSP implicit variables.</section><section name=mixing title="Mixing ATP and JSP"><p><resin/> actually creates a JSP file after processing a ATP file.It then evaluates the JSP file to create the output HTML. So theactual processing order is:<ol><li>Parse ATP file as HTML<li>Find and parse XSL stylesheet<li>Applying the stylesheet to the ATP, creating a JSP file<li>Execute the JSP file</ol><p>Here's we're going to take advantage of this by creating a namedcounter. If the counter has an 'id' attribute, we'll use it at thevalue of the application variable.<p>Scripts use the counter the same as before:</p><example title='counter.atp'><eg-em>A counter example: </eg-em><counter id='test'/></example><p>Here's the patterns to do it. The <code/xsl-cache/> directivetells <resin/> that it can cache the generated JSP file. <resin/> cancache the file because it only depends on the ATP file, not on therequest or on a random number.<example title='default.xsl'><?xsl-cache?><xsl:template match='counter'> <# var id = elt.attribute.id if (id) { #> <%= application.attribute["<#= id #>"] %> <# } else { #> <%= application.attribute.counter++ %> <# } #></xsl:template></example><p>The following JSP file is the result. <resin/> willexecute the generated JSP file to produce the result. Becausedefault.xsl was marked as cached, on following requests <resin/> willmerely reexecute 'gen438.jsp'.<results title='gen438.jsp'>A counter example: <%= application.attribute["test"]++ %></results></section><section name=clever title="Using Clever Patterns">The previous example can be rewritten by using more clever <ahref='xql.html'>XQL</a> patterns. One pattern matches counterswith id attributes. Another matches other counters. So the followingstylesheet does the same thing as the previous one.<example><?xsl-cache?><xsl:template match='counter'> <%= application.attribute.counter++ %></xsl:template><xsl:template match='counter[@id]'> <%= application.attribute["<#= elt.attribute.id #>"] %></xsl:template></example></section>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -