首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在SSIS的XML任务中使用/实现exsl函数节点集()

在SSIS的XML任务中使用/实现exsl函数节点集()
EN

Stack Overflow用户
提问于 2009-06-10 08:09:32
回答 1查看 2.2K关注 0票数 0

我试图将XSL转换应用于SSIS包XML任务中的XML文件。

这一切都很好,但不幸的是,由于我需要使用node-set()函数,我的XSL比普通的“可移植性”稍差一些。我的XSL的一个简化示例是:

代码语言:javascript
运行
复制
<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>

我在样式表声明中使用了以下名称空间:

代码语言:javascript
运行
复制
xmlns:msxsl="urn:schemas-microsoft-com:xslt"

这在VS、XMLSpy (只要我将XSLT设置为MSXML)等方面都有效。但是,当我试图在包中执行XMLSpy时,会得到以下异常:

Error: 0xC002F304在XML,XML :一个错误与以下错误消息:"Function‘msxsl:node()’已失败“。

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

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

我正在调用一个模板来实现EXSLT :split函数,以便将一个字符串“tokenise”到它的组成元素中。"Kermit T Frog“将按以下方式返回:

代码语言:javascript
运行
复制
<token>Kermit</token>
<token>T</token>
<token>Frog</token>

它存储在变量$familyNames中,然后我将遍历该变量。但是,由于这是作为结果树片段返回的,所以我需要用msxsl: node set ()函数包装它,以便将结果作为节点集处理。不知道如何才能达到以上的目的。

下面是我从http://www.exslt.org/获得的str:拆分的实现

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-06-16 02:37:57

我想出了一个解决方案,它涉及使用自定义脚本任务而不是XML任务来转换XML。脚本任务中的代码是:

代码语言:javascript
运行
复制
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:拆分模板实现。我已经将Microsoft msxml命名空间声明替换为以下内容:

代码语言:javascript
运行
复制
xmlns:exsl="http://exslt.org/common"

直接调用str:拆分函数(不需要将其存储在变量中)。

我所知道的唯一含义是,我需要将Mvp.Xml安装到安装SSIS的服务器的GAC中(详见这里)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/974248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档