How to split a large XML document in many smaller ones?

How to split a large XML document in many smaller ones?

by Minollo
Posted on September 19, 2008 0 Comments

We often receive this kind of question: "I have a large XML document and I need to split it in many smaller documents; my document looks like this:[cc lang="xquery"] field1.1 field2.1

[/cc]...and I need to documents that don't contain more than N records each." Using DataDirect XQuery this task is quite simple; leveraging the ddtek:serialize-to-url() function, you can do something like this: [cc lang="xquery"] declare variable $recordsPerDocument := 10; let $records := doc("c:/books.xml")/records let $groupCount := xs:integer(fn:ceiling(count($records) div $recordsPerDocument)) for $g in 1 to $groupCount let $group := $records[fn:position() gt ($g - 1) * $recordsPerDocument and fn:position() le $g * $recordsPerDocument] return ddtek:serialize-to-url( <records>{ $group }</records>, concat("file:///c:/split-", $g, ".xml"), "indent=yes") [/cc]What if you want to do something similar, but applied to RDBMS tables? How do I split the content of a RDBMS table across multiple XML documents? Well, as we are talking about DataDirect XQuery, it shouldn't surprise you that basically the same XQuery can be applied to a table:[cc lang="xquery"] declare variable $recordsPerDocument := 10; let $records := collection("myTable")/myTable let $groupCount := xs:integer(fn:ceiling(count($records) div $recordsPerDocument)) for $g in 1 to $groupCount let $group := $records[fn:position() gt ($g - 1) * $recordsPerDocument and fn:position() le $g * $recordsPerDocument] return ddtek:serialize-to-url( <records>{ $group }</records>, concat("file:///c:/split-", $g, ".xml"), "indent=yes") [/cc]Once again XQuery offers a simple, flexible solution for a problem that comes up pretty frequently.

Minollo

View all posts from Minollo on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
More From Progress
210x120_iStock-655864390_RITM0094534
Shadow Analytics: Why You Can’t Afford to Leave It Unchecked
Read More
 
232x131_ResourceImage_RITM0087682
Then, Now and Beyond: The Future of Back Office Software
Read More
 
2020 Progress Data Connectivity Report
2020 Progress Data Connectivity Report
Read More