Tải bản đầy đủ - 0 (trang)
3-7. Flattening an XML File to Prepare It for Import

3-7. Flattening an XML File to Prepare It for Import

Tải bản đầy đủ - 0trang

Chapter 3 ■ XML Data Sources



Solution

Use the SSIS and the XML task to process the source file before importing it. The following explains how to apply

an XSLT transform to a source XML file.

1.



Copy a source XML file to a source directory. In this example it is

C:\SQL2012DIRecipes\CH03\XMLToFlatten.Xml, and looks like this:





 John Smith

 3



 Staffs

 4, Grove Drive





. . . Data omitted to save space . . .



 Slow Sid

 7



 Vaucluse

 2, Rue des Bleues









2.



Prepare an XSLT file, which transforms the XML file. In this example, it is named

C:\SQL2012DIRecipes\CH03\FlattenedOutput.Xlt, and looks like this:


xmlns = "http://www.w3.org/TR/xhtml1/strict">































153

www.it-ebooks.info



Chapter 3 ■ XML Data Sources

























3.



Open or create an SSIS package. Add an XML task onto the Control Flow pane.



4.



Double-click the XML task to open it. Change the Operation Type to XSLT (which

change the General pane significantly).



5.



For the Input section, set the SourceType to File Connection. Create a new connection

manager for the Source, which points to your XML data file.



6.



For the Second Operand section, set the SecondOperandType to File connection.

Create a new connection manager for the Source that points to your XSD schema file.



7.



In the Outputsection, set SaveOperationResult to True.



8.



In the OperationResult section, set the DestinationType to File Connection. Create a

new connection manager for the Destination, which creates your resulting flattened

XML document. Be sure to select Create File when defining the connection manager.

Be sure to set OverwriteDestination to True. The dialog box should look something

like Figure 3-9.



154

www.it-ebooks.info



Chapter 3 ■ XML Data Sources



Figure 3-9.  Using the XML task to apply an XSLT transform

9.

10.



Confirm your changes with OK.

Run the SSIS package, and if all goes well, a new destination XML file is created,

named C:\SQL2012DIRecipes\CH03\FlattenedOutput.Xml. It should look like this:







 3

 John Smith

 Uttoxeter

 Staffs

 4, Grove Drive






155

www.it-ebooks.info



Chapter 3 ■ XML Data Sources



. . . Data omitted to save space . . .



 7

 Slow Sid

 Avignon

 Vaucluse

 2, Rue des Bleues








How It Works

Another weapon in the SSIS armory to help you deal with XML data is the XML task. This task has several uses,

but the one that we start with is using it to apply an XSLT transform to an XML document. There are many—very

many—possible reasons to apply an XSLT transform, but here we only look at “flattening” an XML document.

This means reducing the complexity of the source file to make it easier to load. As you can see, the XML produced

is much simpler, thanks to the work performed by an XML style sheet.

Of necessity, applying such transforms requires reasonable knowledge of XML and XSLT, or the willingness

to learn how XSLT functions. As the aim here is not to provide a crash-course on the subject of XSLT, the example

is very simple. Armed with the principle, you can then apply it to your own specific business requirements. There

are, however, many excellent books and articles on this subject for you to consult should you need to pursue the

subject further.

Other frequent uses for XSLT when preparing source files include:





Slicing large source files into smaller, more manageable files.







Removing data.







Deleting references to schemas.



Hints, Tips, and Traps





As was the case with flat files and spreadsheets, I suggest that it is probably not worth

defining package-level connection managers for such single-use connections as XML files.



3-8. Importing XML Data from Very Large Files, Putting

a Priority on Speed

Problem

You want to import large XML data files as fast as possible because you have challenging SLAs (service-level

agreements) that you have to meet.



Solution

Use the SQLXML Bulk Load executable to load the data, as it is nearly always the fastest available option. The

following explains how you use it.



156

www.it-ebooks.info



Chapter 3 ■ XML Data Sources



1.



Download and install SQLXML 4.0, unless it is already installed (see the upcoming

note on this).



2.



Locate an XML source data file. In this example, it is the

C:\SQL2012DIRecipes\CH03\Clients_Simple.Xml file.



3.



Create a destination table. The following is the one to use in this example

(C:\SQL2012DIRecipes\CH03\tbl Client_XMLBulkLoad.Sql):

CREATE TABLE CarSales_Staging.dbo.Client_XMLBulkLoad

(

ID int NULL,

ClientName NVARCHAR(1000) NULL,

Address1 NVARCHAR(1000) NULL,

Town NVARCHAR(1000) NULL,

County NVARCHAR(1000) NULL,

Country NUMERIC(18, 0) NULL

);

GO



4.



Create an XML schema file. Note the extensions that are part of the Microsoft mapping

schema (C:\SQL2012DIRecipes\CH03\SQLXMLBulkLoadImport_Simple.Xsd):


xmlns:sql = "urn:schemas-microsoft-com:mapping-schema">














type = "xsd:integer" sql:field = "ID" />




type = "xsd:string" sql:field = "Address1" />


type = "xsd:string" sql:field = "Town" />


type = "xsd:string" sql:field = "County" />


type = "xsd:decimal" sql:field = "Country" />

















157

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

3-7. Flattening an XML File to Prepare It for Import

Tải bản đầy đủ ngay(0 tr)

×