Tambourine

Hi,

I have the following problem:


Here is the fragment of the script:
<xsl:template match="some-template">
<xsl:variable name="xslScript">...</xsl:variable>
<xsl:variable name="xmlFile">...</xsl:variable>
<xsl:variable name="startMode">...</xsl:variable>

... Here I need to call stylesheet $xslScript for $xmlFile file in mode $startMode to retrive some node set...
</xsl:template>

How can it be implemented using the Microsoft XSL transformer



Re: XML and the .NET Framework Calling the external XSL script from the main XSL script

Martin Honnen

Well the tools the XSLT language offers are xsl:include/xsl:import to include/import stylesheet modules and then xsl:call-template and xsl:apply-templates. As you have a secondary XML input file you also need the document function. So with standard XSLT 1.0 you can do

Code Snippet

<xsl:include href="someStylesheetModule.xsl"/>

<xsl:template match="some-element">

<xsl:apply-templates select="document('file.xml')/*" mode="some-mode"/>

</xsl:template>

As you can see in that approach it is not possible to use a variable for the included stylesheet URL, the include is static. The mode attribute value is also a static name, again it is not possible to use a variable there.

Does that help Or are you looking for a way to extend the functionality that standard XSLT provides If so please tell use whether you want to use MSXML or XslTransform or XslCompiledTransform.






Re: XML and the .NET Framework Calling the external XSL script from the main XSL script

Dimitre_Novatchev - MSFT

As Martin has pointed out, the XSLT 1.0 and 2.0 spec does not allow to use a dynamic name (contained in a xsl:variable) or dynamic mode to call a template.

This can be achieved by using <xsl:apply-templates.../>.

For detailed description of how this technique is used to implement the equivalent of higher-order functions in XSLT 1.0 and in XSLT 2.0 see:

http://fxsl.sf.net

the hope page of FXSL -- the Functional Programming Library for XSLT.

I'd recommend to start with the two conference papers first.

Hope this helped.

Cheers,

Dimitre Novatchev





Re: XML and the .NET Framework Calling the external XSL script from the main XSL script

Tambourine

Unfortunately it won't help Sad

I have some index.xml file which contains the references to other data-<i>.xml files and their corresponding data-<i>.xsl scripts. So I can obtain the the name of the file to process and the script to use only while processing index.xml using index.xsl. That's why I can not use such things as xsl:include/xsl:import. All I need is to generate some text (the caption of document section) from data-<i>.xml using corresponding data-<i>.xsl in mode caption and add it to the result tree of index.xml processing.

If there's any solution that involves using of JScript please let me know.

I can use whether XslTransform or XslCompiledTransform - it doesn't matter.

Thanks in advance.





Re: XML and the .NET Framework Calling the external XSL script from the main XSL script

Martin Honnen

I don't think the .NET XSLT processors allow you to specify a start mode when you run a transformation.

If you want to dyamically run an XSLT transformation then one way seems to write an extension object that performs the task, here is a prototype:

Code Snippet

class Example

{

public XPathNavigator Transform(string xmlInput, string stylesheet)

{

XslCompiledTransform xsltProcessor = new XslCompiledTransform();

xsltProcessor.Load(stylesheet);

XmlDocument resultDocument = new XmlDocument();

using (XmlWriter writer = resultDocument.CreateNavigator().AppendChild())

{

xsltProcessor.Transform(xmlInput, null, writer);

}

XPathNavigator result = resultDocument.CreateNavigator();

result.MoveToRoot();

return result;

}

}

then set that up with e.g.

Code Snippet

XslCompiledTransform xsltProcessor = new XslCompiledTransform();

xsltProcessor.Load(@"stylesheet.xslt");

XsltArgumentList xsltArgs = new XsltArgumentList();

xsltArgs.AddExtensionObject(@"http://example.com/ex1", new Example());

then in the stylesheet you can do e.g.

Code Snippet

<xsl:stylesheet

version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:ex1="http://example.com/ex1">

...

<xsl:apply-templates select="ex1:Transform('file.xml', 'sheet.xsl')/*"/>

...

</xsl:stylesheet>

But there are lot of issues I see, security is one problem, and resolving of those relative URLs another one. And you also want a start mode which is another issue that approach does not solve.