xsl-list
[Top] [All Lists]

Re: Including multiple XML documents with associated stylesheets

2003-11-20 15:53:18
Alex,

While XSLT specifies how transformations are written and how they operate, it doesn't address how to invoke a processor to run a transformation on a document. Even the <?xml-stylesheet?> processing instruction, a sort-of standard that is recognized across XSLT-capable clients such as IE and Mozilla, isn't actually part of XSL, but is described in a separate Recommendation. There are good reasons for this: XSLT, it is hoped, should be able to perform in a wide range of different kinds of architectures.

So what you've asked about, how to get a transformation to include into its result the results of running other transformations on external documents, seems like a fair enough thing to want (it could be a pretty powerful feature). But XSLT proper doesn't do this, and it isn't something that any vendor of an XSLT processor has provided for, to my knowledge.

In your example, since there is no name clash between elements in files-list.xml and test-file.xml, you could simply import or include test-file.xslt into iterate-files.xslt, and it would start working for you. The way to avoid name clashes, should there be any, would be to mark the templates in test-file.xslt with their own mode, which would then be called when you process test-file.xml. Unfortunately this approach would not support the "dynamic" assignment of stylesheets to sub-documents you appear to want -- which set of templates (whether with a mode or not) is to be applied to which subdocument would have to be known ahead of time and hard-coded into iterate-files.xslt. (It could, to be sure, be switched by a template that would match the top-level elements of your sub-documents, if they were distinct; but there are a number of 'ifs' here.)

If you must have run-time assignment of stylesheets to sub-documents, the clean way to do it in XSLT would probably be an extension function that would return the result of the transformation of a given source document with a given stylesheet -- in principle you could copy this into the result, or even process over it again (assuming it's returned as a node set) with the local templates. I'm not sure you want to try this, however -- it depends not only on how much responsibility you want to take on for extension development and maintenance (with all the portability issues etc.), but also on why you're trying to achieve this.

Stepping back a level, and not trying to invoke the separate transformations from XSLT but rather from your calling environment, would be a very reasonable fallback to try.

Cheers,
Wendell

At 03:45 PM 11/20/2003, you wrote:
I found a helpful message in the archives by Jenni Tennison on this one, but
unfortunately it doesn't address the matter of XSL stylesheets in
sub-documents, rather than using the transformation templates of the
governing document.

I have also searched through Google on "multiple XML documents" and various
related filters, to no avail. Accordingly, I will perform the separate
transformations and recombination in Javascript, unless anyone here can
point me to a better alternative?

Regards,

Alex


----- Original Message -----
From: "Alex Hildyard" <alexhildyard(_at_)hotmail(_dot_)com>
To: <XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Wednesday, November 19, 2003 1:55 PM
Subject: [xsl] Including multiple XML documents with associated stylesheets


>
> Hi,
>
> I've looked through the FAQ but haven't found a solution to this problem:
> I'm trying to include multiple XML documents, and each sub-document has
its
> own stylesheet. What I find is that I can load each sub-document in
> isolation and its associated stylesheet is applied without problems
> (test-file.xml), but the stylesheet is blatantly ignored when I try to
load
> it as part of a collection of documents (files-list.xml). My understanding
> is that the individual stylesheets' markup isn't being applied because
I've
> never explicitly requested it to be applied, but I'm not quite sure
exactly
> what "apply-templates" argument I would want in this case?
>
> I'm enclosing the files below, to make things a bit clearer:
>
> Many thanks,
>
> Alex
>
> [files-list.xml]
> <?xml version="1.0" encoding="utf-8" ?>
> <?xml-stylesheet type="text/xsl" href="iterate-files.xslt" ?>
> <!-- This top-level document holds references to the set of external
> files that should be marked up on this page. It can also hold
> any scripts that should be globally accessible
> -->
> <html>
> <head />
> <body>
> <files>
> <file src="test-file.xml"></file>
> <file src="test-file2.xml"></file>
> </files>
> </body>
> </html>
>
>
> [iterate-files.xslt]
> <?xml version="1.0" encoding="UTF-8" ?>
> <xsl:transform version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="html"/>
> <!--
> This template iterates through the file list defined within
> a <files> element and processes the set of external XML files
> contained within it
> -->
> <xsl:template match="files">
> <xsl:for-each select="file">
> <xsl:for-each select="document(@src)">
> <xsl:copy-of select="test" />
> </xsl:for-each>
> </xsl:for-each>
> </xsl:template>
> </xsl:transform>
>
> [test-file.xml]
> <?xml version="1.0" encoding="utf-8" ?>
> <?xml-stylesheet type="text/xsl" href="test-file.xslt" ?>
> <test>
> <div>
> This tag shouldn't appear
> </div>
> </test>
>
> [test-file.xslt]
> <?xml version="1.0" encoding="UTF-8" ?>
> <xsl:transform version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="html"/>
> <!-- All the directives in this file seem toget ignored -->
> <xsl:template match="/">
> <xsl:apply-templates />
> </xsl:template>
> <!-- Template to prove directives in this file are processed:
> replace the contents of the <div> with a new string
> -->
> <xsl:template match="div">
> This text should appear
> </xsl:template>
> </xsl:transform>


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
======================================================================


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