Oliver
Oliver

Reputation: 60

Saving each node of a xml document as a file with XProc

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:

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?

Upvotes: 1

Views: 559

Answers (3)

grtjn
grtjn

Reputation: 20414

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!

Upvotes: 0

Vojtěch Toman
Vojtěch Toman

Reputation: 354

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.

Upvotes: 2

Oliver
Oliver

Reputation: 60

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>

Upvotes: 1

Related Questions