[xsl] How to write modular/extensible Xslt?

Subject: [xsl] How to write modular/extensible Xslt?
From: Manuel Holtgrewe <purestorm@xxxxxxxxxxx>
Date: Tue, 23 Dec 2003 22:51:57 +0100
Hi

I have a problem with writing a modular Xslt sheet for merging trees:

In our "node definition files" we have some <node:definition /> tags
that may have children "node:definition" tags. However, you can also
include such tags using "node:include" tags with the attribute 
"definition".

Consider the following:

== including.ndf.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<node:definition
    xmlns:node="http://www.binarycloud.com/ns/node";
    version="2.0" id="top">
  <children>
    <node:include definition="included.ndf.xml" id="including">
      <param name="foo">bar</param>
      <param name="newkey">yet another value</param>
    </node:definition>
  </children>
</node:definition>
==

== included.ndf.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<node:definition
    xmlns:node="http://www.binarycloud.com/ns/node";
    version="2.0" id="included">
  <param name="foo">oldvalue</param>
  <param name="oldkey">some value</param>
</node:definition>
==

Now, I want to include the included.ndf.xml and merge the root tag
"node:definition" from included file with the "node:include" file
(including tag has is overriding). This is not a big problem and I 
already have a template for this using recursive "xsl:call-template"
calls:

== merge.xsl fragment
<!--
  node:include tags are threated specially: The two trees are
  to be merged.
-->
<xsl:template match="*[name() = 'node:include']">
    <!-- try to retrieve the included ndf -->
    <xsl:param name="bc-app-path" />
    <xsl:variable name="path" select="concat($bc-app-path,@definition)" />
    <xsl:variable name="ndf" select="document($path)" />

    <!-- get shortcuts to the including and included node -->
    <xsl:variable name="including" select="." />
    <xsl:variable name="included" select="$ndf/node:definition" />
        
    <!-- Merge the including node:include and the included 
         node:definition tags to a node:definition tag. -->
    <xsl:call-template name="merge-include">
        <xsl:with-param name="nodes1" select="$included" />
        <xsl:with-param name="nodes2" select="$including" />
    </xsl:call-template>
</xsl:template>
== 

Where the template merge-include merges all param and section tags.
"section" tags are simple, named containers for param tags.

Now, my question: How can I keep such a template modular so I can add
other tags to merge, too. We use different namespaces for different
packages of our framework and the package "form" introduces a namespace
"form" with new tags. Now, I want to merge one "<form:validator>" list
in each included and including tag for the resulting tag. How can I do
so easily? Is it possible to do so without having to touch the template
every time I add new tags to be merged?


Manuel

-- 
Unix definitely is a user friendly operating system.
- It is only picky with its friends.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread