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

RE: Split into numbered files: without side-effect? (X

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

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.


Joe

http://joe.fawcett.name/


From: Yves Forkl <Y.Forkl@xxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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"?>
<root>
  <a>
    <content>text of a</content>
  </a>
  <b>
    <b1>
      <content>text of b1</content>
    </b1>
    <b2>
      <content>text of b2</content>
    </b2>
  </b>
  <c>
    <content>text of c</content>
  </c>
</root>

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"?>
<chunks>
  <element>a</element>
  <element>b1</element>
  <element>b2</element>
  <element>c</element>
</chunks>

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

chunk_01_a.txt
chunk_02_b1.txt
chunk_03_b2.txt
chunk_04_c.txt

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?

Yves


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://xmlns.srz.de/yforkl/xslt/functions"
xmlns:saxon="http://saxon.sf.net/"
extension-element-prefixes="saxon"
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()"/>
    <xsl:sequence
      select="document('chunk_starting_elements.xml')
                /chunks/element[. = local-name($context-element)]"/>
  </xsl:function>

<xsl:template match="*">

    <xsl:variable name="new-chunk-name"
      select="my:chunk-started-by(.)"/>

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

<xsl:apply-templates/>

</xsl:template>

</xsl:stylesheet>

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.