How to Automatically Generate a File Name for Received Files |
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:
<xsl:value-of select="local-name()"/>
xmlns:System="xalan://java.lang.System"
<xsl:value-of select="System:getProperty('user.home')"/>
xmlns:java="http://xml.apache.org/xalan/java"
<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:value-of select="java:format($sdf, $date)"/>
|