RE: [xsl] splitting one xml into many xml documents using xsl

Subject: RE: [xsl] splitting one xml into many xml documents using xsl
From: Joerg Pietschmann <joerg.pietschmann@xxxxxx>
Date: Mon, 12 Mar 2001 11:26:26 +0100
> Hi,
> 
>   Here is details about my problem. I have the
> folloing adddress.xml document
[snip]
> and I want to split the above xml into the follwing
> xml documents. How can I do that using xsl? 
> 

As various posters already noted, you cannot do that with
pure XSL, but you can use processor-specific extension functions.

The problem with that ist that they all have different and
sometimes unpleasant failure modes, so i want to present an
alternative solution: write everything to one file and use
another tool to split this file.

I use a Unix toolset (Cygwin on WinNT), Perl would be another
possibility.

For your problem i'd use

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="address_object">
    <xsl:for-each select="*">
      <!-- prepare a line with the filename for the splitting tool -->
      <xsl:text>
@ </xsl:text>
      <xsl:value-of select="name()">
      <xsl:text>.xml</xsl:text>
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Command line frame:
saxon -o tmp.xml adress_object.xml split.xsl
awk 'BEGIN {
  output="";
}
/^@ / {
  output=$1;
  print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > output;
}
{
  if( output!="" ) {
    print > output;  
  }
}' tmp.xml
rm tmp.xml

Note that all error checking is omitted, you should probably
check the return value of the XSL processor ond the existence of
the file before invoking the splitter.
There are also a lot of traps despite the simplicity, for example
i can *guarantee* for my files that there will never be a line with
"@" at the beginning except for the file delimiters i write
out, YMMV.

The advantage of this approach is that i can create directories
for the result files to put in, check for duplicate filenames
and for newer files about to be overwritten, write protected files
etc. (That's what i meant with "failure modes" at the beginning.)

HTH
J.Pietschmann
--

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread