[xsl] Transforming XML to multiple different versions (multiple files)

Subject: [xsl] Transforming XML to multiple different versions (multiple files)
From: "Richard Corfield" <rdc@xxxxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 4 Mar 2004 11:28:50 +0000
Hi

I've been struggling with this for a few days now and my XSL skills are very basic at the moment. I have XML that contains multiple chapters and I want to output to multiple files, each of which should contain only one of the <chapter> nodes, but with the rest of the file unchanged. E.g. the input file is of the form:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE book SYSTEM "booksarc.dtd">
<book id="bk117">
<metadata>
  <isbns><isbn ref="bk117">0750307625</isbn></isbns>
  <history><firstpub>1989</firstpub></history>
  <publisher>Institute of Physics Publishing</publisher>
</metadata>
<bookdata>
<title>Measured tones</title>
<subtitle>The interplay of physics and music</subtitle>
<author-group><author aff="bk117a1"><fnm>Ian</fnm><snm>Johnston</snm></author>
<affil id="bk117a1">University of Sydney, Australia</affil>
</author-group>
<contents pdf="bk117v1.pdf">
<chapter type="front" ser="1" head="" start="i" pdf="bk117f1.pdf">
<title>Prelims, List of tables and Prologue</title>
</chapter>
<chapter ser="1" num="1" start="1" pdf="bk117c1.pdf">
<title>Why these and not others?</title>
</chapter>
<chapter ser="2" num="2" start="17" pdf="bk117c2.pdf">
<title>Music and scientific method</title>
</chapter>
<chapter ser="3" head="Interlude 1" start="37" pdf="bk117c3.pdf">
<title>Brass instruments</title>
</chapter>
</contents>
</bookdata>
</book>

and I want to output to separate files of the form:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE book SYSTEM "booksarc.dtd">
<book id="bk117">
<metadata>
  <isbns><isbn ref="bk117">0750307625</isbn></isbns>
  <history><firstpub>1989</firstpub></history>
  <publisher>Institute of Physics Publishing</publisher>
</metadata>
<bookdata>
<title>Measured tones</title>
<subtitle>The interplay of physics and music</subtitle>
<author-group><author aff="bk117a1"><fnm>Ian</fnm><snm>Johnston</snm></author>
<affil id="bk117a1">University of Sydney, Australia</affil>
</author-group>
<contents pdf="bk117v1.pdf">
<chapter type="front" ser="1" head="" start="i" pdf="bk117f1.pdf">
<title>Prelims, List of tables and Prologue</title>
</chapter>
</contents>
</bookdata>
</book>

etc. etc.

I have worked out how to output the <chapter> elements to multiple files, but I'm having trouble copy ing the preceding and following parts of the xml. Here is a snippet of the XSL I have been working with: (I'm using XML::LibXSLT to do the transform)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:template match="/">
  <xsl:apply-templates select="//chapter" />
</xsl:template>

<xsl:template match="chapter">
  <xsl:variable name="docnumber">
    <xsl:number value="position()" format="1"/>
  </xsl:variable>
  <xsl:document href="/iop/ebooks/data/load/tony-dtd/tmp{$docnumber}.html" method="html">
    <xsl:copy-of select="."/>
  </xsl:document>
</xsl:template>

</xsl:stylesheet>

I have left out all my attempts to copy the remaining parts of the xml (including rewriting the default templates and using copy-of on preceding and following nodes). I hope someone can point me in the right direction.

Many thanks

Richard



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


Current Thread