How to automatically generate a file name for received files

Previous Next

This example generates customer specific filenames for files received  in the web service response. The example shows how to build a file path based upon information in the returned XML (1), how the user's home directory can be found from local properties (2) and how a formatted date can be retrieved (3). Using these items, a suitable file name can be made for the received file data.

The transformation is as follows:

<?xml version="1.0" encoding="windows-1252" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="MyMTOMProviderURN" xmlns:System="xalan://java.lang.System" xmlns:java="http://xml.apache.org/xalan/java">

  <xsl:output method="xml" encoding="windows-1252" omit-xml-declaration="no" />

  <xsl:variable name="date" select="java:java.util.Date.new()" />

  <xsl:variable name="sdf" select="java:java.text.SimpleDateFormat.new('---yyyy-MM-dd---HH.mm.ss.SSS')" />

<xsl:template match="*|@*|comment()|processing-instruction()|text()">

<xsl:copy>

  <xsl:apply-templates select="*|@*|text()|comment()|processing-instruction()" />

  </xsl:copy>

  </xsl:template>

<xsl:template match="ns1:FileData">

<xsl:copy>

<xsl:processing-instruction name="usoft-mtom">

  <xsl:text>path="</xsl:text>

  <xsl:value-of select="System:getProperty('user.home')" />

  <xsl:text>/</xsl:text>

  <xsl:value-of select="local-name()" />

  <xsl:value-of select="java:format($sdf, $date)" />

  <xsl:text>.txt"</xsl:text>

  </xsl:processing-instruction>

  </xsl:copy>

  </xsl:template>

  </xsl:stylesheet>

 

The sections of this transformation associated with retrieving items for the file name are described below:

1.The sections of the transformation shown above that are concerned with building a file path based upon information in the returned XML are as follows (with comments):

<xsl:value-of select="local-name()"/>

 

The XML returned from the Web Service is used as part of the file name. Since we do not know the XML response format in this example we just ask for the name of the current node. In practical applications, the query might be something like select="concat(../ABCService/Filename,../ABCService/defaultExtension)"
2.The sections of the transformation concerned with extracting the user's home directory from the local properties are as follows:

xmlns:System="xalan://java.lang.System"

 

Makes the java.lang.System.getproperty() method available for use below.

<xsl:value-of select="System:getProperty('user.home')"/>

 

The System name space is used to invoke a static method of the java.lang.System object.
3.The sections of the transformation that retrieve a formatted date are as follows:

xmlns:java="http://xml.apache.org/xalan/java"

Allows java objects to be created, as below.

<xsl:variable name="date" select="java:java.util.Date.new()"/>

 

Creates a java Date object that holds the current date and time.

<xsl:variable name="sdf" select="java:java.text.SimpleDateFormat.new('---yyyy-MM-dd---HH.mm.ss.SSS')"/>

 

Creates a simple date format to format the Date object into a string.

<xsl:value-of select="java:format($sdf, $date)"/>

 

The formatting of the current date by the simple date format is inserted as part of the desired file name.