<s1 title="XPath Filters">

<p>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.</p>

<example title='default.xsl'>
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

&lt;xsl:template match="section">
  &lt;xsl:apply-templates/>
&lt;/xsl:template>

&lt;xsl:template match="section[@title]">
  &lt;h1>
      &lt;xsl:value-of select="@title"/>
  &lt/h1>
  &lt;xsl:apply-templates/>
&lt;/xsl:template>
</example>

<p>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.</p>

<example title='hello.xtp'>
&lt;section title="First Section">
Some text
&lt;/section>

&lt;section>
Section with no title
&lt;/section>
</example>
<results>
&lt;h1>First Section&lt;/h1>
Some text

Section with no title
</results>

<s2 title="StyleScript">

<p>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.</p>

<example title='default.xsl'>
$template(section) &lt;&lt;
$apply-templates();
>>

$template(section[@title]) &lt;&lt;
&lt;h1>$(@title)&lt/h1>

$apply-templates();
>>
</example>

</s2>

<s2 title="Summary">

<ul>
<li>The XPath pattern <var/a[b]/> selects <var/a/> only if <var/b/> exists.
</ul>

</s2>
</s1>
