Run XSLT transformation on multiple XML files with XProc

202 Views Asked by At

I have a directory with a bunch of files, some of which follow the naming convention 001.xml, 002.xml, 003.xml etc. I also have an XSLT stylesheet that I want to apply on every single of those files, but not other files in the directory. I want to save the output of all these transformations in the directory conversions.

In XProc, I can do each transformation manually with this:

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
    xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0" name="transformations">
    <p:input port="source" sequence="true"/>
    <p:output port="result"></p:output>
    <p:xslt name="first">
        <p:input port="source">
            <p:document href="001.xml"/>
        </p:input>
        <p:input port="stylesheet">
            <p:document href="converter.xsl"/>
        </p:input>
        <p:input port="parameters">
            <p:empty/>
        </p:input>
    </p:xslt>
    <p:store href="conversions/001.converted.xml" method="xml" indent="true"/>
    <p:xslt name="second">
        <p:input port="source">
            <p:document href="002.xml"/>
        </p:input>
        <p:input port="stylesheet">
            <p:document href="converter.xsl"/>
        </p:input>
        <p:input port="parameters">
            <p:empty/>
        </p:input>
    </p:xslt>
    <p:store href="conversions/002.converted.xml" method="xml" indent="true"/>
    <p:xslt name="third">
        <p:input port="source">
            <p:document href="003.xml"/>
        </p:input>
        <p:input port="stylesheet">
            <p:document href="converter.xsl"/>
        </p:input>
        <p:input port="parameters">
            <p:empty/>
        </p:input>
    </p:xslt>
</p:declare-step>

The first two converted files as saved with p:store as intermediate files, and the final one is saved by configuring the result output port in oXygen.

I would like to learn how to do this with XProc on all the files in the directory that follow the \d+\.xml naming convention. I have read the for my level of skills fairly impenetrable XProc documentation numerous times, looked at the existing examples online, and I understand that there are things like p:for-each, p:directory-list with a filtering attribute etc. but for the life of me I can't figure out how to put things together.

I'd be most grateful for your help.

1

There are 1 best solutions below

1
On

To process your directory filtered file list with p:xslt you can use e.g.

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
    xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0" xpath-version="2.0">
    <p:output port="result" sequence="true"/>
    <p:directory-list include-filter="00[0-9].xml" path="."></p:directory-list>
    <p:for-each>
        <p:iteration-source select="//c:file/doc(@name)"></p:iteration-source>
        <p:xslt>
            <p:input port="source"></p:input>
            <p:input port="stylesheet">
                <p:document href="converter.xsl"></p:document>
            </p:input>
            <p:input port="parameters">
                <p:empty/>
            </p:input>
        </p:xslt>
    </p:for-each>
</p:declare-step>