Re: [xsl] Include files

Subject: Re: [xsl] Include files
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sat, 23 Dec 2000 18:51:27 +0000
Steve,

> I want to use some Java script to format the dates, numbers etc.
> However, I want to have a common file that contains the java script
> for all these functions (so I have only one place to make code
> changes). How do I include the java script file in my XSL files so
> that I can call the functions in the java script file from the XSL
> file?.

Under the XSLT 1.1 working draft, you could do this with the xsl:script
element.  The xsl:script element's 'src' attribute takes a URL that
refers to a remote Javascript file that holds your functions.  To use
it, you need to declare a namespace within your stylesheet for
identifying that set of functions.  For example:

<xsl:stylesheet version="1.1"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:foo="http://www.foo.com/"; />
                
<xsl:script language="javascript"
            src="foo.js"
            implements-prefix="foo" />

...

</xsl:stylesheet>

Within the stylesheet, you can then call any of the functions within
an XPath expression.  For example:

  <xsl:value-of select="foo:myFunction(...)" />

So, that's what the answer is if you have an XSLT 1.1 processor, but I
don't know that any processors support this at present.

So, the only way to use Javascript functions in XSLT at the moment is
through Microsoft's extension element, msxsl:script, and this only
allows locally-defined functions to be used, not external ones. There
are two solutions I can think of that you can use now, as long as
you're using MSXML as your processor.

The first option is to define a general entity that points to the
Javascript file, and include it using that entity:

<!DOCTYPE xsl:stylesheet [
<!ENTITY foo SYSTEM 'foo.js'>
]>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:foo="http://www.foo.com/";
                extension-element-prefixes="msxsl" />
                
<msxsl:script language="javascript"
            implements-prefix="foo">
  &foo;
</msxsl:script>

...

</xsl:stylesheet>

The second option is, rather than having a Javascript file to hold
your functions, have a separate XSLT stylesheet to hold them.  The
stylesheet would only hold the xsl:script element, and look like:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:foo="http://www.foo.com/";
                extension-element-prefixes="msxsl" />
                
<msxsl:script language="javascript"
            implements-prefix="foo">
  <!-- your functions here -->
</msxsl:script>

</xsl:stylesheet>

Your main stylesheet could then include or import this utility
stylesheet.  You would still have to make sure that the foo prefix was
declared (with the same namespace URI) in the main stylesheet:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:foo="http://www.foo.com/"; />
                
<xsl:include href="foo.xsl" />

...
                
</xsl:stylesheet>

One advantage of this second approach is that you will be able to
change the utility stylesheet to reflect the new capabilities of XSLT
1.1 and xsl:script as opposed to msxsl:script without touching your
main stylesheet.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread