RE: [xsl] VB 2008 Passing Parameter to XSLT 2.0

Subject: RE: [xsl] VB 2008 Passing Parameter to XSLT 2.0
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 10 Jul 2009 00:30:36 +0100
> HI,
>  trying to pass parameter to my XSLT from a VB application.
> 
> What we have:
> XSLT/Xpath 2.0
> Windows
> VB 2008 .NET 3.5
> 
> As you can see I've  tried with addParam and SetParameter 
> which one should I use, and how?

You seem to be using some strange hybrid of two different APIs here:
Saxon.Api which is used to drive the Saxon XSLT 2.0 processor, and the
System.Xml.Xsl API which is used to drive the Microsoft XSLT 1.0 processor.
If you're using XSLT 2.0 then you're using Saxon, and you want to forget all
about System.Xml.Xsl. The Saxon.Api is documented at 

http://www.saxonica.com/documentation/dotnetdoc/index.html

and the method for setting a parameter on a transformation is

XsltTransformer.SetParameter()

>         Dim xslArg As XsltArgumentList = New XsltArgumentList

You don't need that, that's a System.Xml.Xsl artefact.
> 
>         'transformer.SetParameter(New QName("", "", 
> "target"), New XdmAtomicValue("A Value"))

That's right for Saxon.

>         'xslArg.AddParam("target", "", target)

You don't need that, again it's System.Xml.Xsl stuff.

>         'Create an XmlTextWriter to handle the output.
>         Dim writer As XmlTextWriter = New 
> XmlTextWriter("orderout.xml", Nothing)

You don't need that, it's again a System.Xml thing.

>         Dim serializer As New Serializer()

That's what you need for Saxon.

(Why didn't I implement the Microsoft API in Saxon, you might ask? Several
reasons. (a) It consists of concrete classes rather than interfaces, so it's
not actually possible to achieve plug-compatibility, the closest one could
get is conceptual similarity. (b) It had to be changed/extended for XSLT 2.0
anyway. (c) I simply didn't like the design, with methods having dozens of
overloads with different combinations of parameters - difficult to remember,
difficult to test.)

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 


> 
> Thx for your help, AGAIN
> 
> Michel
> 
> Here a subset of code that I use:
> ---------VB Code
> Imports System.IO
> Imports System.Xml
> Imports Saxon.Api
> Imports System.Xml.XPath
> Imports System.Xml.Xsl
> Private Function ParseFile(ByVal source As String, ByVal target As
> String) As ParseResponse
>         Dim response As New ParseResponse()
>         Dim memStream As New MemoryStream()
>         Dim processor As New Processor()
>         Dim xhtmlDoc As New XmlTextReader(source)
>         Dim xslDoc As New XmlTextReader(myParseProperties.XSL)
> 
>         Dim input As XdmNode =
> processor.NewDocumentBuilder().Build(xhtmlDoc)
>         Dim transformer As XsltTransformer =
> processor.NewXsltCompiler().Compile(xslDoc).Load()
>         Dim xslArg As XsltArgumentList = New XsltArgumentList
> 
>         'transformer.SetParameter(New QName("", "", 
> "target"), New XdmAtomicValue("A Value"))
>         'xslArg.AddParam("target", "", target)
>         'Create an XmlTextWriter to handle the output.
>         Dim writer As XmlTextWriter = New 
> XmlTextWriter("orderout.xml",
> Nothing)
> 
> 
> 
> 
>         transformer.InitialContextNode = input
> 
>         Dim serializer As New Serializer()
> 
>         serializer.SetOutputFile(target)
>         'serializer.SetOutputWriter(Console.Out)
> 
>         response.SourceFile = source
>         response.TargetFile = target
>         response.Timestamp = Now()
> 
>         Try
>             transformer.Run(serializer)
> 
>             response.Message = "Successfully parsed and transformed"
>         Catch ex As Exception
>             response.Message = "Error: " & ex.Message
>         End Try
> 
>         Return response
>     End Function
> --------- XSLT Code
> <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet 
> version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:xhtml="http://www.w3.org/1999/xhtml";
> exclude-result-prefixes="xhtml" >
> <xsl:output method="xml" indent="yes" encoding="windows-1252"
> exclude-result-prefixes="xhtml" ></xsl:output> 
> <xsl:strip-space elements="*"/>
> 
> <xsl:key name="keyrefname" match="ref"
> use="lower-case(normalize-space(.))"/>
> <xsl:variable name="file_gccore" as="document-node()"
> select="document('validated_gccore_terms.xml')" /> 
> <xsl:variable name="file_gctct" as="document-node()"
> select="document('validated_gctct_terms.xml')" /> 
> <xsl:variable name="target"  select="target" />
> 
> <xsl:template match="xhtml:html" exclude-result-prefixes="xhtml" >
> 
> <root>
> <xsl:value-of select="$target"/>
> </root>
> </xsl:stylesheet>

Current Thread