flattening XML to load via SSIS package

扶醉桌前 提交于 2019-12-11 04:39:26

问题


I have an XML output from Lotus Notes DB. Following is the XML structure:

<Exec>
   <project id='C000253' type='Approved' >
      <DaysInOnHold>39</DaysInOnHold>
      <DaysInCompleted>0</DaysInCompleted>
      <ProjectComplexity type='1280'>Complex</ProjectComplexity>
      <ChangeRequest>
         <OnGoingAmt type='768'>-112</OnGoingAmt>
         <EAmt type='768'>123</EAmt>
      </ChangeRequest>
      <ChangeRequest>
         <ItemNbr type='768'>2</ItemNbr>
         <EAmt type='768'>321</EAmt>
      </ChangeRequest>
      <IncidentalItem>
         <ACost type='768'>0</ACost>
         <Billable type='1280'>Billable</Billable>
      </IncidentalItem>
      <IncidentalItem>
         <ACost type='768'>33</ACost>
         <Billable type='1280'>Billable</Billable>
      </IncidentalItem>
      <MaterialItem>
         <AQty type='768'>1</AQty>
         <ItemNbr type='768'>4</ItemNbr>
      </MaterialItem>
      <MaterialItem>
         <AQty type='768'>0</AQty>
         <ItemNbr type='768'>5</ItemNbr>
      </MaterialItem>
   </project>
   <project id='C110011' type='Not Approved'>
     ...
      ...
   </project>
</Exec>

When I load the XML source in my SSIS package, it creates a table for every column name, which of-course is not very desirable.

I would like to transform this in such a way that it gives me the ability to have a Project table, MaterialItem and ChangeRequest tables (which would subsequently be related back to the individual project entry.

Would XSLT be an answer? If so, any help would be appreciated.

I thought I'd add in what the desired output should be (seems that all the type attributes are causing the issues). So seems that i need a transform that would get rid of all the attributes - specifically the type="...."

<Exec>
   <project id='C0001111'>
      <DaysInOnHold>33</DaysInOnHold>
      <DaysInCompleted>0</DaysInCompleted>
      <ProjectComplexity>Complex</ProjectComplexity>
      <ChangeRequest>
         <OnGoingAmt>52</OnGoingAmt>
         <EAmt>123</EAmt>
      </ChangeRequest>
      <ChangeRequest>
         <ItemNbr>2</ItemNbr>
         <EAmt>321</EAmt>
      </ChangeRequest>
      <IncidentalItem>
         <ACost>0</ACost>
         <Billable>Not Billable</Billable>
      </IncidentalItem>
      <IncidentalItem>
         <ACost>33</ACost>
         <Billable>Billable</Billable>
         </IncidentalItem>
      <MaterialItem>
         <AQty>1</AQty>
         <ItemNbr>4</ItemNbr>
      </MaterialItem>
      <MaterialItem>
         <AQty>0</AQty>
         <ItemNbr>5</ItemNbr>
      </MaterialItem>
   </project>
</Exec>

回答1:


This is straight-forward with XSLT. Firstly, you need to read up on the XSLT identity transform which on its own will copy all the nodes and attributes in your XSLT as is.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

If you want to remove just type attributes, but leave other attributes intact, just add the following template to the above XSLT. This will match type attributes, but output nothing (i.e. they will be ignored)

<xsl:template match="@type" />

If you want to remove ALL attributes, simply amend the indentity transform template to remove the match on attributes. For example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>


来源:https://stackoverflow.com/questions/19493789/flattening-xml-to-load-via-ssis-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!