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.
View all posts from Minollo on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.