Hi,
One of the niftier extensions available in the Saxon library is one
that allows a stylesheet to be compiled and applied dynamically.
As its designer undoubtedly anticipated, it can even provide the
basis for at least a lightweight pipelining infrastructure. The
stylesheet below allows the specification of a set of stylesheets to
be run in sequence over the input. Presently the stylesheets are
named in a top-level variable, though of course this spec could also
be external (and more fully-featured). Given the variable
<xsl:variable name="processes">
<m:path>stylesheets/</m:path>
<m:step>one.xsl</m:step>
<m:step>two.xsl</m:step>
<m:step>three.xsl</m:step>
<m:done/>
</xsl:variable>
the stylesheets "stylesheets/one.xsl", "stylesheets/two.xsl" and
"stylesheets/three.xsl" (relative to the pipelining stylesheet) are
run in series over the input document, until either a "done" element
is encountered, or there are no more steps. (The "path" element may
be omitted; if used it must currently appear first.)
My questions:
* This runs (tested under the current Saxon 8.9), but how will it
scale? In particular, Mike Kay may be able to say whether compiled
stylesheets are be cached when this is run over a set of documents.
If not, wouldn't compiling each stylesheet anew for each input
document be an impediment?
* Are there any obvious pitfalls or problems with this approach? (Or
any not so obvious?) How does it compare to other methods?
(So far I can say only that it's really easy to use, but again I'm
not sure it will scale as well as other methods. It does have the
advantage, it appears, of avoiding serialization altogether until the
last step.)
The stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://saxon.sf.net/"
xmlns:m="http://mulberrytech.com/local/xslt"
version="2.0"
extension-element-prefixes="saxon"
exclude-result-prefixes="m">
<xsl:output method="xml"/>
<!-- Saxon pipelining stylesheet:
Takes document input and passes it through a chain of transformations
The steps will run in order until a "done" element is
encountered or they run out
-->
<xsl:variable name="processes">
<m:path>./</m:path>
<m:step>one.xsl</m:step>
<m:step>two.xsl</m:step>
<m:step>three.xsl</m:step>
<m:done/>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="$processes/m:step[1]">
<xsl:with-param name="in" select="/"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="m:step">
<xsl:param name="in"/>
<xsl:param name="stylesheet" select="concat($processes/m:path,.)"/>
<xsl:variable name="out">
<xsl:variable name="process"
select="saxon:compile-stylesheet(doc($stylesheet))"/>
<xsl:apply-templates select="saxon:transform($process,$in)"
mode="annotate">
<!-- 'annotate' mode will provide the result with local
annotations -->
<xsl:with-param name="stylesheet" select="$stylesheet"/>
</xsl:apply-templates>
<!-- If you don't want the result annotated, simply use
<xsl:copy-of select="saxon:transform($process,$in)"/> -->
</xsl:variable>
<xsl:if test="empty(following-sibling::*[1])">
<xsl:copy-of select="$in"/>
</xsl:if>
<xsl:apply-templates select="following-sibling::*[1]">
<xsl:with-param name="in" select="$out"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="m:done">
<xsl:param name="in"/>
<xsl:copy-of select="$in"/>
</xsl:template>
<xsl:template match="/" mode="annotate">
<xsl:param name="stylesheet" select="'not given'"/>
<xsl:comment>
<!-- adding a comment to the top of the result -->
<xsl:text> process run: </xsl:text>
<xsl:value-of select="$stylesheet"/>
</xsl:comment>
<xsl:text>
</xsl:text>
<xsl:copy-of select="node()"/>
</xsl:template>
</xsl:stylesheet>
For testing, my one.xsl, two.xsl and three.xsl stylesheets all look
like this (with adjustments):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:element name="{local-name()}-one">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Comments welcome,
Wendell
======================================================================
Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc. http://www.mulberrytech.com
17 West Jefferson Street Direct Phone: 301/315-9635
Suite 207 Phone: 301/315-9631
Rockville, MD 20850 Fax: 301/315-8285
----------------------------------------------------------------------
Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================