简体   繁体   中英

XML to XSD using SSIS

I am trying to load below XML into SQL Server table using SSIS. Original XML:

<RESULTS>
    <SEARCHRESULT RECORDS="3014">
        <ROW ROWNUM="1" PKField="EntityCounter">
            <COLUMN COLNUM="1" NAME="EntityCounter" DATATYPE="number">1234</COLUMN>
            <COLUMN COLNUM="2" NAME="Reference ID" DATATYPE="string">5678</COLUMN>
            <COLUMN COLNUM="3" NAME="Name" DATATYPE="string">XYZ</COLUMN>
            <COLUMN COLNUM="4" NAME="Entity Type" DATATYPE="string">LMN</COLUMN>
        </ROW>
    </SEARCHRESULT>
</RESULTS>

The destination table has 4 columns: EntityCounter, ReferenceID, Name and EntityType. When I create XSD for such file, I don't get the expected output.
XML source generates four columns at the output: ROW_ID, RWONUM, PKField and SEARCHRESULT_Id.

I was able to load the data when I modified the XML as below: Modified XML:

<RESULTS>
    <SEARCHRESULT RECORDS="3014">
        <ROW ROWNUM="1" PKField="EntityCounter">
            <EntityCounter>1234</EntityCounter>
            <ReferenceID>5678</ReferenceID>
            <Name>XYZ</Name>
            <EntityType>LMN</EntityType>
        </ROW>
    </SEARCHRESULT>
</RESULTS>

When I create and use XSD for above file, XML source generates seven columns at the output: ROWNUM, PKFiled, EntityCounter, ReferenceID, Name, EntityType and SEARCHRESULT_Id.

Is it possible to generate similar output using original XML?

Thank you @MarkWojciechowicz for suggesting an option to use XSLT..! I was able to transform original xml into required format using below xslt code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="COLUMN[@NAME]">
        <xsl:element name="{concat('Col_',translate(@NAME,' ',''))}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Modified XML can be loaded using SSIS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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