Skip to content

Instantly share code, notes, and snippets.

@ndw
Created December 11, 2013 16:25
Show Gist options
  • Save ndw/7913582 to your computer and use it in GitHub Desktop.
Save ndw/7913582 to your computer and use it in GitHub Desktop.
This pipeline copies a ZIP file, renaming "README.md" to "README.txt". It requires XML Calabash 1.0.16 or later (because there are a couple of bugs in earlier versions of the zip/unzip implementations).
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
xmlns:cx="http://xmlcalabash.com/ns/extensions"
xmlns:c="http://www.w3.org/ns/xproc-step"
xmlns:pxp="http://exproc.org/proposed/steps"
name="main" version="1.0">
<p:input port="parameters" kind="parameter"/>
<p:output port="result">
<p:pipe step="zip" port="result"/>
</p:output>
<p:serialization port="result" indent="true"/>
<p:import href="http://xmlcalabash.com/extension/steps/library-1.0.xpl"/>
<pxp:unzip href="testzip.zip"/>
<p:for-each name="loop">
<p:iteration-source select="/c:zipfile/c:file"/>
<p:output port="docs">
<p:pipe step="unzip" port="result"/>
</p:output>
<p:output port="entries">
<p:pipe step="entry" port="result"/>
</p:output>
<pxp:unzip name="unzip" href="testzip.zip" content-type="application/octet-stream">
<p:with-option name="file" select="/c:file/@name"/>
</pxp:unzip>
<p:choose>
<p:xpath-context>
<p:pipe step="loop" port="current"/>
</p:xpath-context>
<p:when test="/c:file/@name = 'README.md'">
<p:identity>
<p:input port="source">
<p:inline><c:entry name="README.txt"/></p:inline>
</p:input>
</p:identity>
</p:when>
<p:otherwise>
<p:add-attribute attribute-name="name" match="/c:entry">
<p:input port="source">
<p:inline><c:entry/></p:inline>
</p:input>
<p:with-option name="attribute-value" select="/c:file/@name">
<p:pipe step="loop" port="current"/>
</p:with-option>
</p:add-attribute>
</p:otherwise>
</p:choose>
<p:add-attribute name="entry" attribute-name="href" match="/c:entry">
<p:with-option name="attribute-value" select="base-uri(/c:data)">
<p:pipe step="unzip" port="result"/>
</p:with-option>
</p:add-attribute>
</p:for-each>
<p:wrap-sequence name="manifest" wrapper="c:zip-manifest">
<p:input port="source">
<p:pipe step="loop" port="entries"/>
</p:input>
</p:wrap-sequence>
<pxp:zip name="zip" href="/tmp/newzip.zip">
<p:input port="source">
<p:pipe step="loop" port="docs"/>
</p:input>
<p:input port="manifest">
<p:pipe step="manifest" port="result"/>
</p:input>
</pxp:zip>
</p:declare-step>
@LeifW
Copy link

LeifW commented Feb 20, 2020

Ah, it was the href="testzip.zip" stuff it had a problem with. If I change those to e.g. href="file:///tmp/testzip.zip", it works. Though, I wonder if there's a way to use relative paths with this stuff?

@LeifW
Copy link

LeifW commented Feb 20, 2020

Adding a <p:with-option name="href" select="resolve-uri('newzip.zip', document-uri())"/> seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment