2

I made an XSL script that used msxml:script for use with XMLCompiledTransform, but the people who need it said the script wouldn't work on their Linux/Perl environment (that's literally as much as I know about how they're using the XSL) because it "uses Microsoft specific extensions". So I'm trying to make the XSL more neutral by using xsl:script. However, I'm having difficulty making it work.

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:theScript="urn:CustomScript" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xsl theScript fo xs fn">
<xsl:output method="xml" indent="yes" version="1.0" encoding="utf-8" omit-xml-declaration="no"/>
  <xsl:script language="javascript" implements-prefix="theScript">
      <![CDATA[
      function test() {return "test"; }
      ]]>
  </xsl:script>
  <xsl:template match="/">
    <test>
      <xsl:value-of select="theScript:test()" />
    </test>
  </xsl:template>
</xsl:stylesheet>

The above gives me the error "Cannot find the script or external object that implements prefix 'urn:CustomScript'."

If I get rid of xmlns:theScript="urn:CustomScript" it gives me the error "Prefix 'theScript' is not defined."

I've also tried removing all traces of "theScript" prefix and just using implements-prefix="local" but that doesn't work either. It tells me test is an unknown XSLT function.

So am I just doing something wrong here, or does XMLCompiledTransform not support xsl:script?

Bob
  • 7,851
  • 5
  • 36
  • 48

2 Answers2

2

xsl:script is only defined in XSLT1.1, but this standard was cancelled in favor of XSLT2.0, so realistically if you're looking for portability, I wouldn't recommend using it.

The most portable form of XSLT is definitely XSLT1.0, as there's far more support for it. Although some things are definitely easier in XSLT2.0, you'll probably find most 'script' code you might want to do can be done in just XSLT alone. XSLT2.0 can define functions, but you can emulate functions using named templates in XSLT1.0, it's just a bit more cumbersome.

XSLT2.0:

<xsl:template match="/">
  <xsl:value-of select="test('param')" />
</xsl:template>

<xsl:function name="test" as="xs:string">
  <xsl:param name="param" as="xs:string" />
  <xsl:text>Test</xsl:text>
</xsl:function>

XSLT1.0:

<xsl:template match="/">
  <xsl:call-template name="test">
    <xsl:with-param name="param" select="'param'" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="test">
  <xsl:param name="param" />
  <xsl:text>Test</xsl:text>
</xsl:template>

The parameter's not used in this example however, but you get the idea.

Flynn1179
  • 11,925
  • 6
  • 38
  • 74
  • You forgot to mention that XSLT 1.1 has never been an official document of W3C. – Dimitre Novatchev Jul 09 '11 at 14:31
  • 1
    No, I didn't, but if you want to be pedantic about it, it *is* an 'official' document, it's just never been a recommendation, it's officially 'Retired' (http://www.w3.org/TR/#tr_XSLT). As I'm sure you're fully aware many tools are developed based on a spec that's only in the 'Working Draft' stage, so it's not unreasonable to expect there are one or two tools out there that support it, although nobody should recommend using it. – Flynn1179 Jul 09 '11 at 14:51
  • And I am sure that you include the text of this comment in your answer it would become much more useful. – Dimitre Novatchev Jul 09 '11 at 14:54
1

There is no xsl:script instruction in XSLT.

There is no XMLCompiledTransform class that is part of NET.

The existing XslCompiledTransform class that comes with .NET in the System.Xml.Xsl namespace implements a compliant XSLT 1.0 processor -- as such it doesn't support any instruction xsl:script.

Recommendation: To achieve true portability of an XSLT application, don't use any (inline or not) extension functions or undocumented, non-mandatory or non-compliant features.

While a number of XSLT 1.0 processors support EXSLT, the XslCompiledTransform processor only supports the node-set() EXSLT extension function.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431