views:

118

answers:

1

I have a visual basic file with a number of functions I want to use inside my XSL. I can build it as a dll and reference in my XSL project but I don't know how to reference it in my XSL file. I am using visual studio.

I get the error "Cannot find the script or external object that implements prefix..."

I want to do something like:

xmlns:mylib="urn:extnlib.dll"

Has anyone done this?

A: 

Hi Ben

You need to pass in the extension using XsltArgumentList.

As an example:

Dim xmlPath As String : xmlPath = sArgs(0)
Dim xsltPath As String : xsltPath = sArgs(1)
Dim dllFileName As String : dllFileName = sArgs(2)

Dim xsltReader As XmlTextReader = New XmlTextReader(xsltPath)
Dim xsltDoc As XslCompiledTransform = New XslCompiledTransform()

xsltDoc.Load(xsltReader, New XsltSettings(), New XmlUrlResolver())

Dim xslArg As XsltArgumentList = New XsltArgumentList()

Dim dll As Assembly = Assembly.LoadFrom(dllFileName)

For Each objType As Type In dll.GetTypes()
    If objType.IsPublic Then
        Dim ext As Object = Activator.CreateInstance(objType)
        xslArg.AddExtensionObject("urn:" & dllFileName & "#" & objType.Name, ext)
    End If
Next

Dim xpathDoc As XPathDocument = New XPathDocument(New XmlTextReader(xmlPath))
Dim outWriter As XmlWriter = XmlWriter.Create(Console.Out)

xsltDoc.Transform(xpathDoc, xslArg, outWriter)

outWriter.Flush()
outWriter.Close()

My extension is in XsltExtensions.dll, within which I have:

Public Class Simple
    Public Function DateFormat(ByVal dateToParse As String, ByVal format As String) As String
        DateFormat = DateTime.Parse(dateToParse).ToString(format)
    End Function
End Class

So putting it all together:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:my="urn:XsltExtensions.dll#Simple" exclude-result-prefixes="my">

<xsl:template match="/">
  <root>
    <xsl:value-of select="my:DateFormat('3/9/2002', 'yyyy-MM-dd')"/>
  </root>
  </xsl:template>
</xsl:stylesheet>
Carlos da Costa