cancel
Showing results for 
Search instead for 
Did you mean: 

Set file name with XSLT - receiver channel - elements from XML?

peter_wallner2
Active Contributor
0 Kudos

Dear experts,

I have an XSLT mapping for a message that is sent via a receiver channel to a customer.

The customer wants a certain file name which is put toghether by elements from the XML message.

I have looked at:

The java code and the XSLT work perfectly!

In the line


fos = new FileOutputStream(new File("Fixed_" + new Date().getTime() + ".xml"));

a fixed value, date and time are used for the file name.

But I need some values from the XML message to create my name. But how do I do that? Can I somehow get values from the XML message through that java function or do I have to adapt the XSLT?

Can someone please help me!

Thank you,

Peter

Accepted Solutions (0)

Answers (1)

Answers (1)

udo_martens
Active Contributor
0 Kudos

Hi Peter,

you can either replace the "Fixed_" by a XML field value using Java DOM: [Java DOM De- Mystified: Starter Kit|https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/8871] [original link is broken] [original link is broken] [original link is broken];

or use a second variable reading a XML element in the receiver adapter to create the file name: [Configuring the Receiver File/FTP Adapter |http://help.sap.com/saphelp_nw2004s/helpdata/en/bc/bb79d6061007419a081e58cbeaaf28/content.htm]

Regards,

Udo

peter_wallner2
Active Contributor
0 Kudos

Hi Udo,

Thank you! I would go for DOM because I will have to use a Seeburger X.400 receiver channel and that is not as flexible as the File Adapter.

Question: The DOM code I can just embed into my java mapping - is that correct?

Best regards,

Peter

udo_martens
Active Contributor
0 Kudos

Hi Peter,

yes, there two different approaches to map XML with Java: DOM and SAX. Assumedly it is easier to select just one certain field value with DOM.

Regards,

Udo

peter_wallner2
Active Contributor
0 Kudos

Thank you Udo,

I will need two values from the payload and assume DOM will be easier because I can "jump" from element to element. I will try this tonight and report about my "achievements"

Best regards,

Peter

Edited by: Peter Wallner on Jul 22, 2010 1:28 PM

peter_wallner2
Active Contributor
0 Kudos

Hi Udo, Hello experts,

Now I do have a problem. I am using this XSL file:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:map="java:java.util.Map"
  xmlns:dyn="java:com.sap.aii.mapping.api.DynamicConfiguration"
  xmlns:key="java:com.sap.aii.mapping.api.DynamicConfigurationKey">
 
<xsl:output indent="no" />
<xsl:param name="inputparam"/>
 
 
<xsl:template match="/">
 
    <!-- change dynamic configuration -->
    <xsl:variable name="dynamic-conf"  
        select="map:get($inputparam, 'DynamicConfiguration')" />
    <xsl:variable name="dynamic-key"   
        select="key:create('http://sap.com/xi/XI/System/File', 'FileName')" />
  
    <xsl:variable name="dummy" 
        select="dyn:put($dynamic-conf, $dynamic-key, 'TOOLDATA_.xml')" /> 
  <xsl:copy-of select="." />
</xsl:template>
 
</xsl:stylesheet>

But then the output file is only named "TOOLDATA_.xml". So I assume the XSL is working.

And I am using this Java-file:


package pi_mappings;
import com.sap.aii.mapping.api.*;
import java.io.*;
import java.util.*;
 
public class SetFileName extends AbstractTransformation
{
	static final int BUFFER = 1024*1000;
    private Map param;
    public SetFileName(){  }
	public void setParameter (Map map)
	{
		param = map;
		if (param == null)
		{
			param = new HashMap();
		}
	}
	public static void main(String args[])
	{
		try
		{
			InputStream in = new FileInputStream(args[0]);
			OutputStream out = new FileOutputStream(args[1]);
			SetFileName sfn = new SetFileName();
			//sfn.setParameter(mapOfMap);
			sfn.execute(in, out);
		}
		catch (Exception e)
			{
				e.printStackTrace();
			}
	}
	
    public void execute(InputStream inputstream, OutputStream outputstream)
    throws StreamTransformationException {
		try
		{			
			DynamicConfiguration conf = (DynamicConfiguration) param.get("DynamicConfiguration");
			DynamicConfigurationKey KEY_FILENAME = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
			
			int len = 0;
			byte buf[] = new byte[BUFFER];
			FileOutputStream fos = null;
 
			if (conf != null)
			{
				fos = new FileOutputStream(new File(conf.get(KEY_FILENAME) + new Date().getTime()) ); //sample only 
			}
			else
				fos = new FileOutputStream(new File("Fixed_" + new Date().getTime() + ".xml"));
			
 
				while ((len = inputstream.read(buf)) > 0)
				{
					outputstream.write(buf, 0, len);
					fos.write(buf, 0, len);
				}
				fos.close();		
 		}
		catch(Exception e){ e.printStackTrace();}
    }
	
	public void transform(TransformationInput arg0, TransformationOutput arg1)
	throws StreamTransformationException {
		getTrace().addInfo("JAVA Mapping Called");
	this.execute(arg0.getInputPayload().getInputStream(), arg1
		.getOutputPayload().getOutputStream());
	}
	
}

Could you please help me find the mistake. I tried to test it locally by changing the main-method but I could not get it to work.

Thank you very much for any ideas!

Best regards,

Peter