SSIS创建XML文件的问题

时间:2014-07-15 19:08:39

标签: sql sql-server xml ssis

请告诉我在第一行中使用Tile获取XML文件的最佳方法是:

<?xml version="1.0" encoding="ISO-8859-15" ?>

然后是数据行?

我正在使用的SQL查询在本文末尾。

SSIS是这样的:

  1. 使用附加的查询执行SQL并将结果集映射到变量。
  2. 使用脚本任务在第一行添加标题。
  3. 我想在SQL中,当我对这两行进行注释时,SQL会运行并将输出转换为XML:

        depgate as DepartingGate,
        arrgate as ArrivalGate,
    

    如果我不评论上述行,我收到错误。我得到的错误信息是:

      

    Msg 9420,Level 16,State 1,Line 4   XML解析:第1行,第439字符,非法xml字符。

    我尝试限制&-等,但它不起作用。

    我看到了这个表,它有一些XML的非法字符,但我不确定如何限制这些字符。你能告诉我应该对此有什么好处吗?

     DECLARE @XmlOutput XML
        set @XmlOutput = (
        select
        sched_local_fltdate as FlightDate,
        flt_num as FlightNumber,
        Dep as Origin,
        Arr as Destination,
        ltrim(rtrim(substring(ac, 2, 10))) as AccountNumber,
        ac as RegisttrationNumber,
        actype,
        CASE WHEN cancelled = 'Y' then 'Cancelled'
        when flight_type = 'S' then 'Scheduled'
        when flight_type = 'O' then 'Spare Block'
        when flight_type = 'C' then 'Charter'
        when flight_type = 'F' then 'Ferry'
        when flight_type = 'M' then 'Military'
        when flight_type = 'SR' then 'Re-route'
        when flight_type = 'X' then 'Maintenance Block'
        else LTRIM(RTRIM(flight_type))
        end as FlightTypeDescription, 
        case when cancelled = 'Y' then 'Cancelled'
            when VPART_BLON > 0 then 'Arrived'
            when flight_type in ('O','X') then 'No Flight'
        end as FlightStatusDescription,
        out_utc as OutUTMS,
        in_utc as InUTMS,
        depgate as DepartingGate,
        arrgate as ArrivalGate,
        departure_fuel_lbs as DepartureFuelLBS,
        arrival_fuel_lbs as ArrivalFuelLBS,
        'N/A' as FuelUnitOfMSRName
        from [dbo].[ufn_Get_Flight_Operations_Base_Dataset] (cast(dateadd(day, -2, getdate()) as date), cast(getdate() as date), DEFAULT, DEFAULT, DEFAULT)
    
        for xml Path('row'), elements)
    
        Select @XmlOutput as XMLValue
    

1 个答案:

答案 0 :(得分:1)

您可以编写一个相当简单的编码函数来逃避XML繁琐的5个字符,然后通过该函数运行这些字段。或者使用链接调用REPLACE。 XML使用者需要正确处理转义字符。

这是我们在一段时间内为一位客户转发大型XML文档中的敏感字符所做的一次。

CREATE FUNCTION dbo.uf_XmlEncode (@as_input NVARCHAR(4000))
    RETURNS NVARCHAR(4000)
    WITH EXECUTE AS CALLER
AS

BEGIN

DECLARE @ls_text NVARCHAR(4000)

SET @ls_text = @as_input

SELECT @ls_text = REPLACE( @ls_text , '&', '&amp;' )
SELECT @ls_text = REPLACE( @ls_text , '''', '&apos;')
SELECT @ls_text = REPLACE( @ls_text , '"', '&quot;')
SELECT @ls_text = REPLACE( @ls_text , '>', '&gt;'  )
SELECT @ls_text = REPLACE( @ls_text , '<', '&lt;'  )

    RETURN COALESCE (@ls_text, @as_input)
END
GO

解码正好相反。