From: Steve B. <Ste...@zv...> - 2002-03-17 21:26:29
|
Dear Robert, You wrote: > * I need to have elements like this: > > <Include File="Echo.xml"/> > > and have the "compiler" find Echo.xml in a search path of > directories. I don't see any way XInclude can do that, so > I have the tool doing an initial (internal) transformation > on the include tree that looks for these elements and > converts them to the correct XInclude element using an > extension function that finds the correct directory. Does > this seem like the correct approach. None of the inclusion mechanisms (external entities, document(), XInclude) fail gracefully, so you do indeed need to have higher-level mechanism. There are two ways to do it: have the framework do the search and pass in the filename/URI as a parameter or use an extension function. We've done it either way in various projects. My latest effort uses extensions and I've cut it down to the barest primitives - I have a function that checks to see if a resource exists and another to list subresources. You can get a long way with just those two. To do this I register an extension in TclXSLT that calls out to a couple of very simple Tcl procedures. The Tcl package is attached. Here's the setup Tcl code: package require xslt package require resources ::xslt::extension add http://www.zveno.com/resources ::resources set doc [::dom::libxml2::parse <Test/>] set styledoc [::dom::libxml2::parse $stylesheetxml] set style [::xslt::compile $styledoc] set result [::xslt::transform $style $doc] puts [::dom::libxml2::serialize $result] My stylesheet ($stylesheetxml) looks like this: <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:resource='http://www.zveno.com/resources' extension-element-prefixes='resource'> <xsl:template match='/'> <xsl:text>The directory /tmp does </xsl:text> <xsl:choose> <xsl:when test='resource:exists("/tmp")'/> <xsl:otherwise> <xsl:text>not </xsl:text> </xsl:otherwise> </xsl:choose> <xsl:text> exist</xsl:text> </xsl:template> </xsl:stylesheet> NB. the 'resource' XML namespace declaration and the attribute 'extension-element-prefixes' set up the extension. This ties in with the '::xslt::extension' command in the Tcl code. Now the XML Namespace and the Tcl namespace are tied together. Using the extension approach, rather than pre-process to convert your Include elements into xi:include elements you could just have a template that matches Include and uses the document() function to read in the external file. For example, <xsl:template match='Include'> <xsl:call-template name='processFile'> <xsl:with-param name='dirList' select='"/tmp /var/tmp /usr/local/share/lib"'/> <xsl:with-param name='filename' select='.'/> </xsl:call-template> </xsl:template> <xsl:template name='processFile'> <xsl:param name='dirList'/> <xsl:param name='filename'/> <xsl:variable name='thisDir' select='substring-before($dirList, " ")'/> <xsl:choose> <xsl:when test='not($dirList)'> <!-- No more directories to search --> </xsl:when> <xsl:when test='resource:exists(concat($thisDir), "/", $filename))'> <xsl:apply-templates select='document(concat($thisDir), "/", $filename))'/> </xsl:when> <xsl:otherwise> <xsl:call-template name='processFile'> <xsl:with-param name='dirList' select='substring-after($dirList, " ")'/> <xsl:with-param name='filename' select='$filename'/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> > * All the "standard" tools seem to expect you to use one stylesheet > to process multiple documents. My application wants to take > one document and process it with multiple stylesheets. I expect > I'll just have to write the tool to do that. That's essentially the job of a Content Management System (CMS). They have varying degrees of sophistication. In fact, developing a simple CMS is one of my major projects at the moment. For a really simple approach you can simply use Makefiles. You may also wish to keep an eye on the XML Pipeline Note submitted recently to the W3C. I've also written XSL stylesheets that acts as Makefiles, see the doc subdirectory of the XSLT Standard Library. HTHs, Steve Ball -- Steve Ball | XSLT Standard Library | Training & Seminars Zveno Pty Ltd | Web Tcl Complete | XML XSL Schemas http://www.zveno.com/ | TclXML TclDOM | Tcl, Web Development Ste...@zv... +---------------------------+--------------------- Ph. +61 2 6242 4099 | Mobile (0413) 594 462 | Fax +61 2 6242 4099 |