[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Grouping Question

Subject: Grouping Question
From: "Danny Leblanc" <leblancd@xxxxxxxxxxxxxxxxxxx>
Date: Mon, 11 Jun 2007 14:49:14 -0400
 Grouping Question
Hello everyone,

  Let say I have an XML file that looks like this

<root>
  <L1>Data</L1>
  <L1>Data</L1>
  <L1>Data</L1>
  <L1>Data</L1>
  <L1>Data</L1>
  <L1>Data</L1>
  <L1>Data</L1>
  <L1>Data</L1>
</root>

  What I want to do is split this into multiple files each time a new L1 is
found. The following code is what I use. (The code for this is more or less
generic except for the XPATH).

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:for-each select="root/L1">
         <xsl:result-document
href="C:\\out\\{format-number(position(),'000000000')}.xml">
          <reportrun>
          <batch>
              <xsl:apply-templates select="."/>
          </batch>
          </reportrun>
         </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

  This works A1, just the way I want it to. Now what I would like to do is add
an option that would allow the user to do the same split but they could choose
how many "L1" would go into the output file. For example, right now the above
case creates 8 files, one per L1. I would like to output 4 files that would
each contain 2 L1 nodes.

  I tried something like this

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:for-each-group select="root/L1" group-by="L1[position() mod 5 =
0]">
         <xsl:result-document
href="C:\\out\\{format-number(position(),'000000000')}.xml">
          <reportrun>
          <batch>
              <xsl:apply-templates select="."/>
          </batch>
          </reportrun>
         </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

  Which did not work. Any insights as to what I would have to change to get
this going would be appreciated.

Thank you in advance.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.