Category Archives: XML and XSLT

Place to put XML, XSLT and XPath related articles an stories that I’ve found helpful.

Mask FpML with xslt Identity Transform

This script will copy a FpML xml file and match and transform the fpml:tradeid to add the current date to the node value.

<xsl:stylesheet version="2.0" 

<!--K Caulfield, Sep 2018
	Stylesheet to transform FpML XML messages into FpML XML messages with the trade id details prefixed to make then unique for repeated testing -->

	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	<xsl:strip-space elements="*"/>

<!-- define the prefix we want to use for setting up a text cycyle -->
<!--<xsl:variable name="trade_prefix" select="'20180925_01_'" />-->
<xsl:variable name="trade_prefix" select="concat(fn:format-date(fn:current-date(),'[Y0001][M01][D01]'),'_','01','_')" />
<!-- Identity Transform -->
<xsl:template match="@* | node()">
      <xsl:apply-templates select="@* | node()"/>

<xsl:template match="//fpml:tradeHeader/fpml:partyTradeIdentifier/fpml:tradeId[../fpml:partyReference/@href='party1']/text()">
	<xsl:call-template name="kc:prefix"><xsl:with-param name="nodeVal" select="." /></xsl:call-template>

<xsl:template name="kc:prefix">
	<xsl:param name="nodeVal" />
		<xsl:value-of select="concat($trade_prefix,$nodeVal)" />


Script to transform with saxon .net

Nice DOS script to transform the most recent xml file in a directory, using net.sf.saxon.Transform

REM Script to call Saxon xslt Parser for most recent file in XML directory and save results to HTML/Output.html
rem get the latest file in the XML directory
for /f "delims=" %%i in ('dir /od /b *.xml') do (
	Set recentXMLFile=%%i
CD ..
Set xmlFile="XML\%recentXMLFile%"
Set xsltFile=GU_UI_NoNULLv2.xslt
Set outFile=HTML\Output.html
java -cp saxonHE9-8-0-8j\saxon9he.jar net.sf.saxon.Transform -t -s:%xmlFile% -xsl:%xsltFile% -o:%outFile% --suppressXsltNamespaceCheck:on
IF ERRORLEVEL 1 goto :bad  
REM now open the html file
start "" %outFile%
goto :end
REM deal with error level
echo A Bad Thing Happened

Transform a folder of XML files

If you ever need to bulk transform a directory of xml files, and output each transformed file into a new directory then Saxon has a great command line interface tool that will do just that, in one simple line!

CD C:\Users\caulfiek\Testing java -jar c:\Users\caulfiek\XSLT\saxonHE9-8-0-8j\saxon9he.jar -s:sourceDir\ -xsl:FPML_Masking.xslt -o:targetDir
Here I have  a stylesheet that performs an identity transform on a xml file, but uses xpath to find and mask certain xml node values (like customer name and transaction number).
Download the saxonHE9 cli tool from here:
Create a directory “sourceDir” and “targetDir”
Fill your “sourceDir” with lots of XML files and set up the command as above on your DOS command prompt.