Subject: [xsl] Grouping Question From: "Danny Leblanc" <leblancd@xxxxxxxxxxxxxxxxxxx> Date: Mon, 11 Jun 2007 14:49:14 -0400 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] [ANN] Late-breaking Proposals, B Tommie Usdin | Thread | RE: [xsl] Grouping Question, Michael Kay |
[xsl] How to select specific nodes , Bhaskar | Date | [xsl] EZ way to convert XSL-FO to F, Steven Janoff |
Month |