Project

Profile

Help

Support #5339 » batch.xslt

Johan Gheys, 2022-02-19 15:46

 
<?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:file="http://expath.org/ns/file"
xmlns:saxon="http://saxon.sf.net/"
xmlns:shared="urn:shared-functions"
exclude-result-prefixes="#all"
extension-element-prefixes="saxon"
version="2.0">
<xsl:param name="input-folder" as="xs:string"/>
<xsl:variable name="train-xml" select="shared:open-files($input-folder, 'train_*.xml')"/>

<xsl:template name="xsl:initial-template" match="/">
<xsl:variable name="startTime" select="saxon:timestamp()"/>
<xsl:sequence select="$startTime[shared:always-false()]"/>
<xsl:sequence select="shared:log('INFO', 'Batch execution starting...')"/>
<xsl:for-each select="$train-xml">
<xsl:sequence select="shared:log('DEBUG', concat('base-uri()=', base-uri()))"/>
<xsl:sequence select="shared:log('DEBUG', concat('document-uri()=', document-uri()))"/>
</xsl:for-each>
<xsl:sequence select="shared:log('INFO', 'Batch execution succeeded')"/>
<xsl:sequence select="shared:log-execution-time($startTime)"/>
</xsl:template>

<xsl:function name="shared:always-false" as="xs:boolean">
<xsl:sequence select="current-date() lt xs:date('2000-01-01')"/>
</xsl:function>

<xsl:function name="shared:log">
<xsl:param name="level" as="xs:string"/>
<xsl:param name="message" as="item()*"/>
<xsl:message select="serialize($message)"/>
</xsl:function>

<xsl:function name="shared:log-execution-time">
<xsl:param name="startTime" as="xs:dateTime"/>
<xsl:sequence select="shared:log('DEBUG', concat('Execution time: ', (saxon:timestamp() - $startTime) div xs:dayTimeDuration('PT1S'), 's'))"/>
</xsl:function>

<xsl:function name="shared:open-files" as="document-node()*">
<xsl:param name="baseURI" as="xs:string"/>
<xsl:param name="pattern" as="xs:string"/>
<xsl:variable name="uri" select="file:path-to-uri($baseURI)"/>
<xsl:variable name="files" select="collection(concat($uri, '?select=', $pattern))"/>
<xsl:sequence select="shared:log('DEBUG', concat('Nb of loaded files = ', count($files)))"/>
<xsl:for-each select="$files">
<xsl:sort select="document-uri()"/>
<xsl:sequence select="saxon:discard-document(.)"/>
</xsl:for-each>
</xsl:function>

</xsl:stylesheet>
(3-3/3)