1

I got a xml document with follwing structure

<listOfNodes>
    <node name="file1">content1</node>
    <node name="file2">content2</node>
    ...
    <node name="fileN">contentN</node>
</listOfNodes>

I want to create a xproc pipeline with the result:

  • file1.txt with content1
  • file2.txt with content2
  • ...
  • fileN.txt with contentN

My first approach is:

<p:declare-step name="step_1" type="ts:Extract">
        <p:input port="source" />
        <p:filter select="nodes" name="step_1.1" />
         <p:for-each name="step_1.2">
            <p:iteration-source><p:pipe port="result" step="step_1.1"/></p:iteration-source>
            <p:store method="text" >
                 <p:with-option name="href" select="concat('file:/', 'step_1_',iteration-position(),'.txt')"/>
            </p:store>
        </p:for-each>
</p:declare-step>

But I get a DX0006 error on this.

Can anybody help me, please?

Oliver
  • 60
  • 1
  • 8
  • Your approach should essentially have worked. It is only lacking namespace declarations, and iteration-position() needs p: prefix. See my answer below.. – grtjn Mar 27 '14 at 21:13

3 Answers3

2

Provided content1, ..., contentN are well-formed XML, the following pipeline does the trick:

<p:declare-step version="1.0" xmlns:p="http://www.w3.org/ns/xproc">
  <p:input port="source"/>
  <p:for-each>
    <p:iteration-source select="/*/node"/>
    <p:variable name="filename" select="concat(/node/@name, '.txt')"/>
    <p:unwrap match="/*"/>
    <p:store>
      <p:with-option name="href" select="$filename"/>
    </p:store>
  </p:for-each>
</p:declare-step>

If content1, ..., contentN are not well-wormed XML (plain text, multiple sibling elements etc.), then you can wrap them in an XML wrapper element before you apply p:store (or simply don't apply the p:unwrap step). If you don't want to use a wrapper element, then standard p:store will not work (some XProc implementations may support extension attributes on p:store to store only the contents of the document element; but that is another story). One possibility is to use p:xslt instead.

Vojtěch Toman
  • 354
  • 3
  • 5
1

Following Pipeline does the job:

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0">
    <p:input port="source" />
    <p:xslt name="create-document">
        <p:input port="source" />
        <p:input port="stylesheet">
            <p:document href="splitter.xsl" />
        </p:input>
        <p:input port="parameters"><p:empty/></p:input>
    </p:xslt>
    <p:for-each>
        <p:iteration-source>
            <p:pipe step="create-document" port="secondary" />
        </p:iteration-source>
            <p:store>
                <p:with-option name="method" select="'xml'" />
                <p:with-option name="href" select="p:base-uri(/*)" />
            </p:store>
        </p:for-each>

<p:sink>
    <p:input port="source"><p:pipe port="result" step="create-document"/></p:input>
</p:sink>
</p:declare-step>

But I also had to Split the file in the xslt file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="node">
        <xsl:variable name="filename" select="concat(@name,'.txt')" />
        <saveTo><xsl:value-of select="$filename" /></saveTo>
        <xsl:result-document href="{$filename}">
            <node><xsl:value-of select="current()" /></node>
        </xsl:result-document>
    </xsl:template>

    <!-- standard copy template -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template> 
</xsl:stylesheet>
Oliver
  • 60
  • 1
  • 8
0

Just to understand why your code was throwing an error, I tried it myself. But couldn't reproduce your error. This ran just fine in XMLCalabash:

<p:declare-step name="step_1" type="ts:Extract" version="1.0" xmlns:p="http://www.w3.org/ns/xproc" xmlns:ts="ts">
    <p:input port="source" />
    <p:filter select="/*/node" name="step_1.1" />
     <p:for-each name="step_1.2">
        <p:iteration-source><p:pipe port="result" step="step_1.1"/></p:iteration-source>
        <p:store method="text" >
             <p:with-option name="href" select="concat('step_1_', p:iteration-position(), '.txt')"/>
        </p:store>
    </p:for-each>
</p:declare-step>

Note that I had to add p and ts namespace declarations, and I also added p: before interation-position(). I removed 'file:/' for the practical reason that the files were being written in the root of my file-system.

I could find no record of an DX0006 error. Maybe it tried writing files in the root at your machine as well, but was not allowed to?

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35