RE: [xsl] Split into numbered files: without side-effect? (XSLT 2)

Subject: RE: [xsl] Split into numbered files: without side-effect? (XSLT 2)
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Wed, 26 Sep 2007 19:00:39 +0100

Can't you process the XML the "other way"?
As you have four change/element nodes why not process each of these in a for-each or with apply-templates? You then have access to the position() function to help numbering the separate result-documents.


From: Yves Forkl <Y.Forkl@xxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Split into numbered files: without side-effect? (XSLT 2)
Date: Wed, 26 Sep 2007 17:43:13 +0200

Hi all,

until now I have been coping quite well without being able to use any
side effects in XSLT, but for the ridiculously simple task I am trying
to do, I just don't see how to avoid "cheating" by shamefully falling
back on saxon:assign in XSLT 2.

I would like to split the contents of my input file into a series of
chunks, each going into a separate text file, consecutively numbered
in the order in which the chunks are created (and all of the text
nodes should go into the main result document as well). The trouble
comes in from these requirements:

a) The list of elements making up the chunks should be configurable:
   their paths from the root node are to be read from an XML file.

b) The chunks need not be rooted at the same level in the tree.

For instance, assume I have this input document:

<?xml version="1.0" encoding="iso-8859-1"?>
    <content>text of a</content>
      <content>text of b1</content>
      <content>text of b2</content>
    <content>text of c</content>

Another file, named chunk_starting_elements.xml, says for which
element instances to start a new chunk, by giving their local name
(this is just for simplicity's sake, in reality I am using their paths
from the root node):

<?xml version="1.0" encoding="iso-8859-1"?>

The desired result of the transformation is a series of 4 files named


each of them containing the text nodes from the corresponding element
as given in chunk_starting_elements.xml (plus all of it in the main
result document).

While the stylesheet below certainly does the job (provided it's Saxon
which processes it), I wonder how to adapt it into pure, side-effect
free XSLT 2. Any suggestions?


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="";
exclude-result-prefixes="my xs saxon">

<xsl:output method="text" indent="yes"/>

<xsl:variable name="chunk-number" saxon:assignable="yes" select="0"/>

  <xsl:function name="my:chunk-started-by" as="node()*">
    <xsl:param name="context-element" as="node()"/>
                /chunks/element[. = local-name($context-element)]"/>

<xsl:template match="*">

    <xsl:variable name="new-chunk-name"

    <xsl:if test="$new-chunk-name">
        href="{concat('chunk_', format-number($chunk-number + 1, '00'),
                      '_', $new-chunk-name, '.txt')}">
        <saxon:assign name="chunk-number" select="$chunk-number + 1"/>




Current Thread