2009-06-10 32 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)。