caucho
 Copying nodes and attributes


Stylesheets can use xsl:copy to copy nodes from the XTP file to the generated HTML. The most common use of xsl:copy creaes a default rule to copy the input to the output, i.e. the identity transformation.

In the previous examples, the default rule omitted unknown elements. For most XTP pages, a better default rule copies the input to the output. That way the page can use XTP as little as it wants and mix and match JSP and HTML with custom tags.

The following example creates a default identity pattern and a single custom tag. The match pattern "*|@*" matches any element (the "*") and any attribute (the "@*"). The vertical bar is a union operator. The node is copied using xsl:copy and the contents apply the stylesheet recursively.

xsl:apply-templates has an optional select pattern. The selected nodes will have the stylesheet recursively applied. The default pattern selects all children nodes. The identity template needs to select children nodes and attribute nodes. "node()" selects any child nodes and "@*" selects attributes.

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

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

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

</xsl:stylesheet>
hello.xtp
Testing <font color="red"><hello/></font>

<html>
<body>
Testing <font color="red">Hello</font>
</body>
</html>

StyleScript

The StyleScript equivalent for $apply-templates uses the select attribute as its first parameter. $copy acts like any typical element.

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

$template(hello) <<Hello>>

Summary

  • The XPath pattern * matches any element.
  • The XPath pattern @* matches any attribute.
  • The XPath pattern a|b matches pattern a or b
  • xsl:copy copies XML nodes to the output.
  • xsl:apply-templates can select the nodes to evaluate.
  • XTP pages are parsed as HTML, automatically expanding optional tags, like html and body.
  • Specific templates override general templates.
  • Later templates override earlier ones.
  • $copy expands to xsl:copy
  • $apply-templates(foo) expands to <xsl:apply-templates select="foo"/>

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