Showing posts with label XSLT. Show all posts
Showing posts with label XSLT. Show all posts

Wednesday, July 29, 2020

Python: Performing XSLT recursively on a directory of XML files

I've been using https://github.com/ajryan/RptToXml to extract out xml information from Crystal Reports. I then rip through the created xml files with xlst to provide detail information on how the reports are created.  This little python 3.5+ script uses https://www.saxonica.com/download/download_page.xml and will create html files from a directory tree containing xml files.  

TODO: Write an xxl file to convert Crystal Reports to Jasper Reports.


import os
import subprocess
import glob
from pathlib import Path
root_dir = r"C:\crystalxml"
xslFile = r"C:\Saxon\pretty.xsl"

for filename in glob.iglob(root_dir + '/**/*.xml', recursive=True):

 my_parts = filename.split("\\")
 justname = my_parts[-1].split('.')[0]
 my_dir = os.path.dirname(filename)
 newname = fr'{my_dir}\{justname}.html'
 cmd = fr'java -cp C:\Saxon\saxon-he-10.1.jar net.sf.saxon.Transform -t -s:"{filename}" -xsl:{xslFile} -o:"{newname}"'
 subprocess.call(cmd)

Sunday, February 9, 2020

XSLT with carriage returns and line breaks

I needed to translate carriage returns and line feeds to <BR/> elements in an XSLT process.  Sometimes you get hung up how you normally use functions and then hit a language wall that you think should work,

In this case, the solution was to iterate over the selection and output the elements with an appended tag.

<tr><td><xsl:for-each select="tokenize(customerinfo/@comments, '&#xD;&#xA;')">
                <xsl:value-of select="." /><br/>
</xsl:for-each></td></tr>