Friday, 8 November 2013

XML task in SSIS with example

XML task in SSIS

In this article we will learn about XML task in SSIS. XML task is used to validate, modify, extract or even create files in an XML format.

Drag XML task from tool box and drop on Control flow tab as shown in Figure1

clip_image002 Figure 1

Double click xml task which displays XML task editor as shown in Figure 2 which is having only two tabs. In General tab, you need to specify the connections and type of operation should be performed.

clip_image004Figure 2

General tab properties changed based on the OperationType selected. The XML operation types are as follows.

  • Validate: This option allows XML file schema validation against Document Type Definition (DTD) or XML Schema Definition (XSD) binding control documents. XML task with this option makes sure that XML file is in required format.

  • XSLT: The Extensible Stylesheet Language Transformations (XSLT) are a subset of the XML language that enables transformation of XML data.

  • XPATH: This option uses the XML Path Language and allows the extraction of sections or specific nodes from the structure of the XML document. This option will be used to extract data from XML nodes.

  • Merge: This option allows for the merging of two XML documents with the same structure. this option will be used to combine the results of two extracts from different systems into one document.

  • Diff: This option allows us to compare two XML documents to produce a third document called an XML Diffgram that contains the differences between them.

  • Patch: This option applies the results of a Diff operation to an XML document to create a new XML document.

You can specify the XML file source either through direct input or variable or file connection. This is common for all options.

Secondoperation tab specifies source type and source that contains second XML document to complete the selected operation.

Lets us understand XML task with an example which uses validates an XML using validate option

Steps to follow:

  1. Create one dummy xml file as mentioned below. You can create using any available XML editor.
  2. <?xml version="1.0" encoding="utf-16" ?>
    <NewDataSet xmlns="" >
    <table>
        <column1>col1</column1>
        <column2>col2</column2>
        <column3>col3</column3>
        <column4>col4</column4>
        <column5>col5</column5>
        <column6>col6</column6>
    </table>
    </NewDataSet>
  3. Generate XSD/XML schema either through any generate tool or online. I have created below XSD document using online tool saved as text_xml.xsd
  4. <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="NewDataSet">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="table">
              <xs:complexType>
                <xs:sequence>
                  <xs:element type="xs:string" name="column1"/>
                  <xs:element type="xs:string" name="column2"/>
                  <xs:element type="xs:string" name="column3"/>
                  <xs:element type="xs:string" name="column4"/>
                  <xs:element type="xs:string" name="column5"/>
                  <xs:element type="xs:string" name="column6"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
  5. Create a package and add XML task on the control flow design editor.
  6. Open configuration configuration editor and select operation type as validate
  7. Select a source type as direct input and Write the XML. You can specify even through file connection
  8. In OperationResult property in Output tab specify how to capture the result of validation. Add output result to file test.txt and set OverWriteDestination property to true to allows result to be over written.
  9. In SecondOperand tab you need to create file connection with XSD document which is created in Step2
  10. Set validation type as XSD file to validate XML file.

XML task editor should be shown as displayed below after configuration completed.

image

If you execute the package, Task will be executed successfully and turned into Green color and writes true into text.txt file which indicates XML file contains the correct schema as defined by XSD file. If you want you can test giving wrong XML file which writes false into file

Please leave comment if you like the post

4 comments:

  1. Nice!
    wud love to get the walk over to other task types also...

    ReplyDelete
  2. Nice!
    wud love to get the walk over to other task types also...

    ReplyDelete
  3. Hi, I'm a newbie, and I would like to understand how can I load an XML file from URL and how to manage a validation login (because the URL is not public). Thanks in advance.

    ReplyDelete
  4. Thanks for sharing valuable information with us, keep share more updates on MSBI Online Training

    ReplyDelete