'Process all files in a directory and create a file according to file-name

I have a working XSLT-transformation that I need to apply to all files in the specified directory:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:tei ="http://www.tei-c.org/ns/1.0"
   exclude-result-prefixes="xs"
   version="3.0">
   <xsl:strip-space elements="*"/>
   
   <xsl:output method="text" indent="yes"/> 
   <xsl:output omit-xml-declaration="yes"/>
   <xsl:variable name="files" select="collection('C:\Users\KW\Desktop\Interim_56_zerlegt')"/>


 <xsl:template match="/">
    <xsl:result-document href="Interim_56_zerlegt/test.txt" method="text">
Kuerzel; AT/NT; Stelle
<xsl:apply-templates select="//note"></xsl:apply-templates>
    </xsl:result-document>
 </xsl:template>
      
   
   <xsl:template match="//note">
      <xsl:choose>
         <xsl:when test="child::*[1][self::ref[@type='biblical']]">
            <xsl:for-each select="child::*[@type='biblical']/@cRef">
               <xsl:value-of select="."/>
               <xsl:text>;</xsl:text>
            </xsl:for-each>
            <xsl:text></xsl:text>
            <xsl:text>test;</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>&#10;</xsl:text>
            
         </xsl:when>
      </xsl:choose>
   </xsl:template>
   

  
</xsl:stylesheet>

I have a folder of 56 files that I want to run this transformation on. So I managed to create a file for one file (<xsl:result-document href='Interim_56_zerlegt') and now I need to do this for "every file" in there (technically only XML but there's nothing else in there) Is there a way to do this?



Solution 1:[1]

Use an initial template

   <xsl:param name="files" select="uri-collection('file:///C:/Users/KW/Desktop/Interim_56_zerlegt/?select=*.xml')"/>

   <xsl:template name="xsl:initial-template">
     <xsl:apply-templates select="$files ! doc(.)"/>
   </xsl:template>

and start Saxon (?) with e.g -it.

You will also need to adjust the href="Interim_56_zerlegt/test.txt" to e.g. href="Interim_56_zerlegt/result-{position()}.txt" or perhaps href="{document-uri() => replace('\.xml$', '.txt')}".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1