Lecture 24

XSLT continued...

Import vs. Include

<xsl:import href="URI"> <xsl:include href="URI">

Include is like macro-expand, i.e., as if the included file were simply copied into place. Import is like object-oriented override. You can call overridden rules using <xsl:apply-imports/>, allowing an object-oriented approach to template writing.

Priorities

The short version... you can set a template's priority using the priority="expression" attribute.

Rules without this attribute are given default priorities. Mostly this makes sense... simple rules (just an element or an attribute) have default priority 0. Rules with more context have default priority 0.5. Wildcard rules have negative priorities: (prefix:*, -0.25), (*,@*, -0.5). Note that the priority system is actually multi-step -- all imported rules live in a lower priority plane than rules in the importing stylesheet.

New "control" structures...

In addition to xsl:apply-templates, xsl:call-template, and xsl:for-each we have two conditional constructs:

xsl:choose gives a switch or if/else kind of control structure. Tests are evaluted in the order given, and the template associated with the first true test is expanded. Note that if the otherwise clause is omitted, and no test is true, then no output is generated.

<xsl:choose> <!-- (xsl:when+,xsl:otherwise?) --> <xsl:when test="boolean-expression"> <!-- template --> <xsl:when> ... <xsl:otherwise> <!-- template --> </xsl:otherwise> </xsl:choose>

A simple form of this is the xsl:if construct:

<xsl:if test="boolean-expression"> <!-- template --> </xsl:if>

Output

In additional to explicit elements in templates and xsl:text, we can construct an element node programmatically. This is most useful if we need to compute the element tag name at stylesheet interpretation time.

<xsl:element name="QualifiedName" namespace="URI"> <!-- ((xsl:attribute|xsl:attribute-set)*,template --> </xsl:element>

For attributes, we have

<xsl:attribute name="QualifiedName" namespace="URI"> <!-- template --> </xsl:attribute>

the content of an attribute must expand into text nodes only.

The parameter passing mechanism

xsl:template's can take arguments!! The mechanism is ... unusual.

We can place xsl:param elements at the top-level, or nested within xsl:apply-templates or xsl:call-templates elements.

An xsl:param element takes one of two forms:

<xsl:param name="QualifiedName"> <!-- template --> </xsl:with-param>

or

<xsl:param name="QualifiedName" select="expression"/>

We can recover a parameter value using either $name in expressions, or xsl:with-param elements. The xsl:with-param elements are structed like the xsl:param elements, but the value is a default.

What is really happening here is dynamic scoping (which makes sense in a context where the result tree has such a strong affinity to the expansion process) rather than static scoping, as we're more accustomed too.