2009-06-10 42 views
0

我试图将XSL转换应用于SSIS包XML任务内的XML文件。在SSIS的XML任务中使用/实现exsl函数node-set()

一切都很好,但不幸的是,由于我需要使用函数node-set(),所以我的XSL比正常略微“便携”。我的XSL的一个简单的例子是:

<xsl:for-each select="msxsl:node-set($familyNames)/token"> 
    <xsl:call-template name="PersonNameComponent"> 
    <xsl:with-param name="nameComponentType" select="'S'" /> 
    <xsl:with-param name="nameComponentSeqNo" select="number($noOfGivenNames) + position()" /> 
    <xsl:with-param name="nameComponent" select="." /> 
    <xsl:with-param name="nameTypeName" select="$familyName" /> 
    <xsl:with-param name="roleCode" select="$roleCode" /> 
    </xsl:call-template> 
</xsl:for-each> 

我使用以下命名空间在样式表声明:

xmlns:msxsl="urn:schemas-microsoft-com:xslt" 

这只要工作在VS IDE,XMLSpy的(因为我设置的XSLT发动机作为MSXML)等。然而,当我尝试了包,我得到了以下异常内执行XML任务:

Error: 0xC002F304 at XML Task, XML Task: An error occurred with the following error message: "Function 'msxsl:node-set()' has failed.".

我使用VS2005来设计包装,因为它是2005年版的SSIS的。

关于我如何进行的任何想法,非常感谢。

我在调用一个实现EXSLT str的模板:split函数将一个字符串“tokenise”到它的组成元素中,例如,

<token>Kermit</token> 
<token>T</token> 
<token>Frog</token> 

这被存储在变量$ familyNames,我然后遍历:“柯密Ť蛙”将如下被返回。但是,由于这是作为结果返回的树片段,我需要用函数msxsl:node-set()来包装它,以便将结果视为节点集。不知道我还能如何实现上述目标。

这里的STR的实现:分裂,我从http://www.exslt.org/获得:

<xsl:template name="str:split"> 
    <xsl:param name="string" select="''" /> 
    <xsl:param name="pattern" select="' '" /> 
    <xsl:choose> 
    <xsl:when test="not($string)" /> 
    <xsl:when test="not($pattern)"> 
     <xsl:call-template name="str:_split-characters"> 
     <xsl:with-param name="string" select="$string" /> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:call-template name="str:_split-pattern"> 
     <xsl:with-param name="string" select="$string" /> 
     <xsl:with-param name="pattern" select="$pattern" /> 
     </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
<xsl:template name="str:_split-characters"> 
    <xsl:param name="string" /> 
    <xsl:if test="$string"> 
    <token><xsl:value-of select="substring($string, 1, 1)" /></token> 
    <xsl:call-template name="str:_split-characters"> 
     <xsl:with-param name="string" select="substring($string, 2)" /> 
    </xsl:call-template> 
    </xsl:if> 
</xsl:template> 
<xsl:template name="str:_split-pattern"> 
    <xsl:param name="string" /> 
    <xsl:param name="pattern" /> 
    <xsl:choose> 
    <xsl:when test="contains($string, $pattern)"> 
     <xsl:if test="not(starts-with($string, $pattern))"> 
     <token><xsl:value-of select="substring-before($string, $pattern)" /></token> 
     </xsl:if> 
     <xsl:call-template name="str:_split-pattern"> 
     <xsl:with-param name="string" select="substring-after($string, $pattern)" /> 
     <xsl:with-param name="pattern" select="$pattern" /> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <token><xsl:value-of select="$string" /></token> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
+0

$ familyNames从何而来?你可以发布你创建它的XSLT的一部分吗?也许有一种方法可以完全避免node-set()。 – Tomalak 2009-06-10 08:24:20

回答

3

,我想出了一个变通的是涉及使用自定义脚本任务,而不是XML任务转换XML 。在脚本任务的代码是:

Imports System 
Imports Microsoft.SqlServer.Dts.Runtime 
Imports Mvp.Xml.Common.Xsl 

Public Class ScriptMain 

    ' The execution engine calls this method when the task executes. 
    ' To access the object model, use the Dts object. Connections, variables, events, 
    ' and logging features are available as static members of the Dts class. 
    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. 
    ' 
    ' To open Code and Text Editor Help, press F1. 
    ' To open Object Browser, press Ctrl+Alt+J. 

    Public Sub Main() 

     Dts.TaskResult = Dts.Results.Failure 

     If Dts.Variables.Contains("FullSourcePathFileName") AndAlso _ 
      Dts.Variables.Contains("XsltPath") AndAlso _ 
      Dts.Variables.Contains("FullSourceTransformedPathFileName") Then 

      Dim input As String = CType(Dts.Variables("FullSourcePathFileName").Value, String) 
      Dim xsl As String = CType(Dts.Variables("XsltPath").Value, String) 
      Dim output As String = CType(Dts.Variables("FullSourceTransformedPathFileName").Value, String) 

      Try 
       Dim xslt As New MvpXslTransform() 
       xslt.Load(xsl) 
       xslt.Transform(New XmlInput(input), Nothing, New XmlOutput(output)) 

       Dts.TaskResult = Dts.Results.Success 
      Catch ex As Exception 
       Throw 
       ' Look at logging, e.g. Dts.Logging.Log() 
      End Try 
     End If 

    End Sub 

End Class 

我引用Mvp.Xml项目组件(CodePlex上可用),它提供的EXSLT函数.NET实现。作为额外的副作用,这意味着我可以从xsl中删除str:split模板实现。我已经取代了微软的MSXML命名空间声明针对以下方面:

xmlns:exsl="http://exslt.org/common" 

调用STR:分割功能直接(无需将其存储在一个变量)。

我意识到的唯一含义是我需要将Mvp.Xml安装到将安装SSIS的服务器的GAC上(详细信息请参见here)。