Abbey Workshop

XSLT: Splitting an XML File into Multiple Files with XSLT

This tips explains how to split one XML file into several files using XSLT. The techniques and methods for doing this are currently XSLT processor specific. This example uses the Apache Xalan XSLT processor. If you are using Saxon, Sablotron or some other XSLT processor, the steps will be different.

For this example, the following student directory xml file will be split into three separate files. Each student has an id attribute associated with them. This attribute will be used to create a new file for each student.

student_directory.xml

Download source for: student_directory.xml

Enabling the Functionality

To enable the functionality in Xalan, there are a couple of steps you must take.

Add Redirect Extension Namespaces

First, you must add a redirect namespace to your <xsl:stylesheet> element and an additional attribute. For example:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:redirect="http://xml.apache.org/xalan/redirect" extension-element-prefixes="redirect" version="1.0">

You will notice the addition of the redirect namespace and the extension-element-prefixes attribute. This information tells Xalan to make the internal Redirect class and its features available in this style sheet.

Add Elements That Write a File

The next step is to add special elements to your style sheet that are in the redirect namespace. These elements open, close, and write the files created from the style sheet.

<redirect:write> - This element opens, writes, and closes the file it is writing to. Sort of a one stop file creator. Simply wrap this element around whatever you want to write out, and that text will be written to the file you specify. The file name is specified with a file or select attribute. The file attribute takes a string value while the select attribute takes an XPath expression. If the expression in the select resolves to empty, then the file attribute is used.

<redirect:open> and <redirect:close> - These elements allow your control when a file is opened and when it is closed. Both elements have the same attributes as redirect:write but they are typically entered as empty elements. This allows you to open and close files at different points in the style sheet.

append="true/yes"One other point, you can add the append attribute to one of the above elements to append the information your are writing to the file. However, note that if you are using the XML output method, you could get XML version headers anywhere in the document as a header is automatically added each time a write occurs.

For more information take a look at the Redirect class in the Xalan Javadoc documentation. http://xml.apache.org/xalan-j/apidocs/

Here is a example that uses the redirect elements.

student_split.xsl

Download source for: student_split.xsl

The key template here is of course the student template. The redirect:write element creates a file name based on the id attribute. An xsl:variable element is used to append '.xml' to the value of the id attribute.

Since there are no changes to the subelements, they are just copied and written out in their original state.

If you download this style sheet and the XML file and transform the file, you should get a file for each student: 1.xml, 2.xml, and 3.xml. You also get an empty output file for the main style sheet. (Haven't yet figured out a way to prevent this file from being written out.)

For more information, see the following books

Google
  Web abbeyworkshop.com   

Copyright © Abbey Workshop 2006

Music Only Search
The Black MP3 Album
The MP3 Album Lyrics
Wprld Music Plus
Mp3 Songs Blog
Music Search Store
First Music Search
Mega MP3 Blog
E MP3 BLog
My MP3 Diary
Fast MP3 Finder
My Blog MP3
Mp3 Songs Mart
My MP3 Songs Album
Top Music Group
Mp3 Tune Records
Song Album Guide
Music DataDite
MP3 Buying Guide
Top MP3 Downloads
MP3 Tune Festival
FAST MP3 Downloads
MP3 Mart Online
Upper MP3
Premium MP3 Site
Music Data online
Fine MP3 Audio
Cheap Music Albums
Fine mp3 downloads
World MP3 Music Records
Mp3 Music Data Direct
The Fine Mp3
MP3 Files World
Home Music Store
DatMP3
Fat MP3 Audio
MP3 ZigZag
MP3 Songs Direct
MP3 Tune World
E MP3 Log
Boom MP3 Music
Speedy MP3
Top 20 MP3 music
Deep MP3 World
Deep MP3 Download
Best-20 MP3
Top Twenty MP3
Deep MP3
Big MP3 Guide
Deep MP3 Audio
Mp3 R.O.B.
MP3-Pop Online
Greate-20-MP3
MP3 Appear
3T-MP3