| |
XPath filters can match nodes matching a constraint expression, for example
only matching sections with titles. In the following example, sections
with titles are formatted differently than sections without titles. Using
XPath filters, the stylesheet can use different templates to specify the
formatting without using xsl:if.
default.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="section">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="section[@title]">
<h1>
<xsl:value-of select="@title"/>
</h1>
<xsl:apply-templates/>
</xsl:template>
|
A node may match more than one pattern. The full set of XSLT rules
to tell which pattern to use are fairly involved, but if the
stylesheet always puts general patterns first and specific patterns
last, the XSLT engine will select the later pattern. In the above
example, the filtered pattern "section[@title]" will match because
it's more specific than "section". Even if the two patterns were
equally specific, the engine would select "section[@title]" because
it's later in the stylesheet.
hello.xtp
<section title="First Section">
Some text
</section>
<section>
Section with no title
</section>
|
<h1>First Section</h1>
Some text
Section with no title
|
The StyleScript translation is straightforward. The stylesheet just
uses two templates, one with the filter pattern. Because StyleScript
expands to XSL, the priority rules are exactly the same.
default.xsl
$template(section) <<
$apply-templates();
>>
$template(section[@title]) <<
<h1>$(@title)</h1>
$apply-templates();
>>
|
- The XPath pattern a[b] selects a only if b exists.
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|