我有一个可视化的基本文件,其中包含我想在我的XSL中使用的许多函数。我可以在我的XSL项目中将其构建为dll和引用,但我不知道如何在我的XSL文件中引用它。我正在使用visual studio。
我收到错误“无法找到实现前缀的脚本或外部对象......”
我想做类似的事情:
的xmlns:MYLIB = “瓮:extnlib.dll”
有没有人这样做过?
答案 0 :(得分:2)
您需要使用XsltArgumentList传递扩展名。
举个例子:
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()
我的扩展名在XsltExtensions.dll中,我在其中:
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
所以把它们放在一起:
<?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>