简体   繁体   English

如何使用SSIS包导入XML文件?

[英]How do I import XML files using an SSIS package?

I'm trying to import a XML file into SQL server using SSIS. 我正在尝试使用SSIS将XML文件导入SQL Server。 The XML file is structured like this XML文件的结构如下

<?xml version="1.0" encoding="utf-8"?>
<dataset  xmlns="http://developer.cognos.com/schemas/xmldata/1/"  xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<!--
<dataset
    xmlns="http://developer.cognos.com/schemas/xmldata/1/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocation="http://developer.cognos.com/schemas/xmldata/1/ xmldata.xsd"
>
-->
    <metadata>
          <item name="ORDERNUM" type="xs:string" length="26"/>
          <item name="Business Stream" type="xs:string" length="482"/>
          <item name="ORDERNO" type="xs:decimal" precision="8"/>
          <item name="ORDERTYPE" type="xs:string"/>
          <item name="ITEM_NO" type="xs:decimal" precision="8"/>
          <item name="ITEM_NO2" type="xs:string" length="52"/>
          <item name="PG" type="xs:int" precision="1"/>
          <item name="SG" type="xs:int" precision="1"/>
          <item name="LINEDESC" type="xs:string" length="122"/>
          <item name="CUSTNO" type="xs:decimal" precision="8"/>
          <item name="ORDERQNTY" type="xs:decimal" scale="3" precision="77"/>
          <item name="COST_VALUE" type="xs:decimal" scale="5" precision="77"/>
          <item name="EXTD_LIST" type="xs:decimal" scale="4" precision="77"/>
          <item name="EXTD_VALUE" type="xs:decimal" scale="4" precision="77"/>
          <item name="LINE_NO" type="xs:decimal" precision="9"/>
          <item name="TDATE" type="xs:date"/>
          <item name="TQUARTER" type="xs:decimal" precision="1"/>
          <item name="INVOICEDAT" type="xs:date"/>
          <item name="IQUARTER" type="xs:decimal" precision="1"/>
          <item name="ORIGNUM" type="xs:decimal" precision="10"/>
          <item name="ORIGTYPE" type="xs:string" length="22"/>
          <item name="TYPECALC" type="xs:string"/>
          <item name="SEQ" type="xs:int" precision="1"/>
          <item name="INC_IN_COU" type="xs:string"/>
          <item name="COSTMOD" type="xs:string" length="4"/>
          <item name="GROSSMOD" type="xs:string" length="4"/>
          <item name="CHFLOOR" type="xs:string" length="16"/>
          <item name="Group Customer Description" type="xs:string" length="482"/>
          <item name="Sales Area Description" type="xs:string" length="482"/>
          <item name="Sales Area" type="xs:string" length="8"/>
          <item name="Segment / Region Description" type="xs:string" length="482"/>
          <item name="Segment / Region" type="xs:string" length="8"/>
          <item name="ScheduledDespatchDate" type="xs:date"/>
          <item name="Status Sid" type="xs:int" precision="1"/>
          <item name="ShiptoCustomerNumber" type="xs:decimal" precision="8"/>
          <item name="CONT_TYPE" type="xs:string" length="122"/>
          <item name="EST_START" type="xs:date"/>
          <item name="EST_COMPLTN" type="xs:date"/>
          <item name="ACT_START" type="xs:date"/>
          <item name="ACT_COMPLTN" type="xs:date"/>
          <item name="Project_Status" type="xs:string" length="18"/>
    </metadata>
    <data>
        <row>
            <value>SO1897977</value>
            <value>Products &amp; Applications</value>
            <value>1897977</value>
            <value>SO</value>
            <value>731305</value>
            <value>0670800</value>
            <value>67</value>
            <value>3</value>
            <value>3/4&quot; HM10/8 CI Steam TrapBSP</value>
            <value>20021</value>
            <value>1</value>
            <value>62.136</value>
            <value>187.1</value>
            <value>187.1</value>
            <value>1000</value>
            <value>2011-11-17</value>
            <value>4</value>
            <value>2011-11-17</value>
            <value>4</value>
            <value xs:nil="true" />
            <value> </value>
            <value>SO</value>
            <value>1</value>
            <value>Y</value>
            <value>N</value>
            <value>N</value>
            <value>PDQ/KSP</value>
            <value>Other Customers</value>
            <value>Crumb Jonathan</value>
            <value>917</value>
            <value>Southern Division</value>
            <value>STH</value>
            <value>2011-11-17</value>
            <value>70</value>
            <value>60206</value>
            <value xs:nil="true" />
            <value>1900-01-01</value>
            <value>1900-01-01</value>
            <value>1900-01-01</value>
            <value>1900-01-01</value>
            <value xs:nil="true" />
        </row>
        <row>
            <value>SO1897977</value>
            <value>Products &amp; Applications</value>
            <value>1897977</value>
            <value>SO</value>
            <value>799262</value>
            <value>1643100</value>
            <value>164</value>
            <value>60</value>
            <value>1/2&quot; FIG12 Bronze Y Type BSP+ 0.8 SS</value>
            <value>20021</value>
            <value>5</value>
            <value>54.051</value>
            <value>130.2</value>
            <value>130.2</value>
            <value>2000</value>
            <value>2011-11-17</value>
            <value>4</value>
            <value>2011-11-17</value>
            <value>4</value>
            <value xs:nil="true" />
            <value> </value>
            <value>SO</value>
            <value>1</value>
            <value>Y</value>
            <value>N</value>
            <value>N</value>
            <value>PDQ/KSP</value>
            <value>Other Customers</value>
            <value>Crumb Jonathan</value>
            <value>917</value>
            <value>Southern Division</value>
            <value>STH</value>
            <value>2011-11-17</value>
            <value>70</value>
            <value>60206</value>
            <value xs:nil="true" />
            <value>1900-01-01</value>
            <value>1900-01-01</value>
            <value>1900-01-01</value>
            <value>1900-01-01</value>
            <value xs:nil="true" />
        </row>

I have made a XSD file that looks like this 我制作了一个看起来像这样的XSD文件

<?xml version="1.0"?>
<schema xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://developer.cognos.com/schemas/xmldata/1/" xmlns="http://www.w3.org/2001/XMLSchema">
  <element name="dataset">
    <complexType>
      <sequence>
        <element minOccurs="0" name="data">
          <complexType>
            <sequence>
              <element minOccurs="0" maxOccurs="1" name="row">
                <complexType>
                  <sequence>
                      <element name="ORDERNUM" type="string" />
                      <element name="BusinessStream" type="string" />
                      <element name="ORDERNO" type="decimal"/>
                      <element name="ORDERTYPE" type="string"/>
                      <element name="ITEM_NO" type="decimal" />
                      <element name="ITEM_NO2" type="string" />
                      <element name="PG" type="int" />
                      <element name="SG" type="int" />
                      <element name="LINEDESC" type="string" />
                      <element name="CUSTNO" type="decimal" />
                      <element name="ORDERQNTY" type="decimal" />
                      <element name="COST_VALUE" type="decimal" />
                      <element name="EXTD_LIST" type="decimal" />
                      <element name="EXTD_VALUE" type="decimal"/>
                      <element name="LINE_NO" type="decimal" />
                      <element name="TDATE" type="date"/>
                      <element name="TQUARTER" type="decimal" />
                      <element name="INVOICEDAT" type="date"/>
                      <element name="IQUARTER" type="decimal" />
                      <element name="ORIGNUM" type="decimal" />
                      <element name="ORIGTYPE" type="string" />
                      <element name="TYPECALC" type="string"/>
                      <element name="SEQ" type="int" />
                      <element name="INC_IN_COU" type="string"/>
                      <element name="COSTMOD" type="string" />
                      <element name="GROSSMOD" type="string" />
                      <element name="CHFLOOR" type="string" />
                      <element name="GroupCustomerDescription" type="string" />
                      <element name="SalesAreaDescription" type="string" />
                      <element name="SalesArea" type="string" />
                      <element name="SegmentRegionDescription" type="string" />
                      <element name="SegmentRegion" type="string" />
                      <element name="ScheduledDespatchDate" type="date"/>
                      <element name="StatusSid" type="int" />
                      <element name="ShiptoCustomerNumber" type="decimal" />
                      <element name="CONT_TYPE" type="string" />
                      <element name="EST_START" type="date"/>
                      <element name="EST_COMPLTN" type="date"/>
                      <element name="ACT_START" type="date"/>
                      <element name="ACT_COMPLTN" type="date"/>
                      <element name="Project_Status" type="string" />
                  </sequence>
                </complexType>
              </element>
            </sequence>
          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
</schema>

The problem is when I run the task I get the correct number of rows but all the values are null. 问题是当我运行任务时,我得到了正确的行数,但是所有值都为空。 I'm quite new to XML so I suspect it is something really silly, can anyone help? 我对XML还是很陌生,所以我怀疑它确实很愚蠢,有人可以帮忙吗?

This may not be the answer to your problem. 这可能不是您问题的答案。 I used trial and error to find only the cause of the problem. 我使用反复试验来only找出问题的原因。 One of the possible solution seems to be that you need to reconstruct your XML file with the element names as defined in the XSD file. 一种可能的解决方案似乎是您需要使用XSD文件中定义的元素名称来重建XML文件。

I took your XML file and XSD file to create a new SSIS package. 我带了您的XML文件和XSD文件来创建一个新的SSIS包。 I used XML Source within Data Flow Task to read the files. 我在数据流任务中使用了XML Source来读取文件。

When I executed the package, I got the below results. 执行该程序包时,得到以下结果。 All the values were NULL as you had described in your question. 如您在问题中所述,所有值均为NULL。

空值

After looking at the XSD file, I felt that the names you have defined in the elements in XSD files like ORDERNUM and BusinessStream should have corresponding nodes in the XML file but they were missing. 看完XSD文件后,我觉得您在XSD文件中的元素(如ORDERNUMBusinessStream定义的名称应在XML文件中具有相应的节点,但它们丢失了。 So, I changed the first element that had the nodes value to ORDERNUM and BusinessStream . 因此,我将第一个具有节点value元素更改为ORDERNUMBusinessStream

变更的节点

Re-executed the package and this time the values were shown correctly. 重新执行该程序包,这一次正确显示了值。 Note that I changed the values only in the first row and left the second row unchanged. 请注意,我仅更改了第一行中的值,而未更改第二行。 That's why the values are still NULL. 这就是为什么值仍为NULL的原因。

正确值

Hope that helps. 希望能有所帮助。

The second of the two articles below shows how to use an XLST file in SSIS to transform the XML source file into something you can use for your described purpose. 以下两篇文章的第二篇显示了如何在SSIS中使用XLST文件将XML源文件转换为可用于您描述的目的的文件。 I provide two XSLT file contents at the bottom. 我在底部提供了两个XSLT文件内容。

http://sqlserverpedia.com/blog/sql-server-bloggers/loading-xml-using-ssis/

http://blog.hoegaerden.be/2011/04/20/loading-complex-xml-using-ssis/ http://blog.hoegaerden.be/2011/04/20/loading-complex-xml-using-ssis/

Repairs to listed data sample: 修复列出的数据样本:

1) Remove the space in what appears to be a comment at the beginning: '...xmldata/1/ xmldata.xsd...' to 'xmldata/1/xmldata.xsd' 1)删除开头似乎是注释的空格:“ ... xmldata / 1 / xmldata.xsd ...”至“ xmldata / 1 / xmldata.xsd”

2) Add </data></dataset> to the very end of the data. 2)将</data></dataset>到数据的最后。

You can test it here (Hmmmm, it doesn't seem to work there!): 您可以在此处进行测试(嗯,它似乎在这里不起作用!):

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

Or add this as the second line of XML to test in IE by opening the XML file: 或通过打开XML文件将其添加为要在IE中进行测试的XML的第二行:

<?xml-stylesheet type="text/xsl" href="Cognos.xsl"?>

Look over the xslt listings carefully and note the differences in the 'output' element. 仔细查看xslt清单,并注意'output'元素中的差异。 Note that I needed to define in my XSLT file the namespace that was used in the XML file's 'dataset' element, and give it a name which I can use to prefix references to the nodes defined within 'dataset'. 请注意,我需要在XSLT文件中定义XML文件的“数据集”元素中使用的名称空间,并为其命名,以便为在“数据集”中定义的节点的引用添加前缀。 Assume case sensitivity matters everywhere because it probably does. 假设大小写敏感在任何地方都很重要,因为它可能确实如此。 Do some reading on XSLT elements at: 在以下位置阅读XSLT元素:

http://www.w3schools.com/xsl/ http://www.w3schools.com/xsl/

Cognos.xsl to list contents as 'pipe' (vertical bar) delimited file. Cognos.xsl以“管道”(竖线)定界文件列出内容。 There will be an empty column at the end - due to the trailing delimiter - which you will need to ignore in your code that uses the file. 由于尾随定界符,结尾处将有一个空列,您将需要在使用该文件的代码中忽略该列。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:cog="http://developer.cognos.com/schemas/xmldata/1/">
 <xsl:output method="text" encoding="UTF-8" indent="no" 
  omit-xml-declaration="yes"
/>

  <xsl:template match="/">
    <xsl:apply-templates select="cog:dataset/cog:metadata/cog:item">
    </xsl:apply-templates>
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:apply-templates select="cog:dataset/cog:data/cog:row">
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="cog:dataset/cog:metadata/cog:item">
      <xsl:value-of select="@name"/>
    <xsl:text>|</xsl:text>
  </xsl:template>

  <xsl:template match="cog:dataset/cog:data/cog:row">
    <xsl:text>&#13;&#10;</xsl:text>
      <xsl:apply-templates select="./cog:value">
      </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="cog:dataset/cog:data/cog:row/cog:value">
      <xsl:value-of select="."/>
    <xsl:text>|</xsl:text>
  </xsl:template>

</xsl:stylesheet>

Cognos.xsl to list contents as HTML table: Cognos.xsl将内容列出为HTML表:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:cog="http://developer.cognos.com/schemas/xmldata/1/">
 <xsl:output method="html" encoding="UTF-8" indent="yes" 
/>

  <xsl:template match="/">
    <html><body>
    <table border="1"><tr>
    <xsl:apply-templates select="cog:dataset/cog:metadata/cog:item">
    </xsl:apply-templates>
    </tr>
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:apply-templates select="cog:dataset/cog:data/cog:row">
    </xsl:apply-templates>
    </table>
    </body></html>
  </xsl:template>

  <xsl:template match="cog:dataset/cog:metadata/cog:item">
    <td>
    <xsl:value-of select="@name"/>
    </td>
  </xsl:template>

  <xsl:template match="cog:dataset/cog:data/cog:row">
    <tr>
    <xsl:apply-templates select="./cog:value">
    </xsl:apply-templates>
    </tr>
  </xsl:template>

  <xsl:template match="cog:dataset/cog:data/cog:row/cog:value">
    <td>
    <xsl:choose>
      <xsl:when test=". !='' and . !=' '">
        <xsl:value-of select="." />
      </xsl:when>
      <xsl:when test=". =' '">
        <xsl:text>space</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>null</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    </td>
  </xsl:template>

</xsl:stylesheet>

And, yes, you are all welcome for this concise collection of requirements, fixes, links, and techniques which I have spent nearly 14 hours researching. 而且,是的,我们都花了将近14个小时来研究这些需求,修复,链接和技术的简洁集合,我们都对此表示欢迎。 Maybe I will turn this in to an article. 也许我将其转为一篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM