caucho
 Default Attributes with Filter Patterns


XPath filter patterns can simplify default attributes. Instead of using some sort of if pattern, an XPath pattern can match elements with a specific attribute. In the CSS class attribute example, the stylesheet can have separate templates for an example with a class attribute and one without it.

An XPath filter looks like foo[test]. The test expression is evaluated with foo as the context node. The following example uses example[@class] to match only examples with class attributes.

The test expression can be any XPath expression. If it's a node expression like @class, the filter will match if it can select any matching node.

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

<xsl:template match="example">
  <table class="example">
    <tr>
      <td>
        <xsl:apply-templates/>
      </td>
    </tr>
  </table>
</xsl:template>

<xsl:template match="example[@class]">
  <table class="{@class}">
    <tr>
      <td>
        <xsl:apply-templates/>
      </td>
    </tr>
  </table>
</xsl:template>

</xsl:stylesheet>

In the example stylesheet, an <example> tag will match the second template if it has a class attribute. It will match the first template if it has no class attribute.

The example XTP does not specify class, so the stylesheet will use "example" as the default class.

hello.xtp
<example>
This is an example.
</example>

<table class="example">
<tr>
  <td>This is an example</td>
</tr>
</table>

Summary

  • foo[test] is an XPath filter pattern.
  • Filter patterns match both the path and the filter expression.
  • If the filter is a selection path, it matches if it selects any node.

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