cancel
Showing results for 
Search instead for 
Did you mean: 

XI 3.0: "Flattening" hierarchical structures

Former Member
0 Kudos

Does anybody have a "generalized" method to flatten a hierarchical structure?

In other words, given a XML-typical structure with trees and leaves, how can i generate a old-style-cobol-like flat file using mapping?


H-
 |_A1_
     |_B1   ----->     HA1B1
     |_B2   ----->     HA1B2
 |_A2_                 HA2B3
     |_B3

I've been able to do that using a recursive manipulation of the context (via custom and standard functions), but this seems very dumb because you need to put a function in fron of each field...

Maybe some XSLT-guru knows a better method....

thanks in advance.

Alessandro

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Alessandro

Assuming your input XML looks like this,

<?xml version="1.0" encoding="UTF-8" ?>

<H>

<A1>

<B1>HA1B1</B1>

<B2>HA1B2</B2>

</A1>

<A2>

<B3>HA2B3</B3>

<B4>HA2B4</B4>

</A2>

</H>

And the output you want is,

HA1B1HA1B2HA2B3HA3B4

You can try the following XSLT code,

<?xml version='1.0' ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">

<xsl:value-of select="H/A1/B1"/>

<xsl:value-of select="H/A1/B2"/>

<xsl:value-of select="H/A2/B3"/>

<xsl:value-of select="H/A2/B4"/>

</xsl:template>

</xsl:stylesheet>

The above code can be fine-tuned further depending on how your input file looks exactly.

cheers

Sameer

Former Member
0 Kudos

Thanks.

The difference from what you propose are:

1) H is a Header with different fields, as An and Bn: I know that there are 3 levels (H,A and B) but not the number of occurrencies at each level

2) the result is not a non-structured stream but should be like a fixed-fields fixed-length recordset with more rows (number of rows = Sum of n of 3rd levels)

Former Member
0 Kudos

Send me a sample input file, so that i can have a look at it and propose a solution for it.

Former Member
0 Kudos
<?xml version="1.0" encoding="UTF-8" ?><ZSDM_STAGCOLLTEMA><IDOC BEGIN="1"><EDI_DC40 SEGMENT="1"><TABNAM>EDI_DC40</TABNAM><MANDT>201</MANDT><DOCNUM>0000000000045022</DOCNUM><DOCREL>46C</DOCREL><STATUS>30</STATUS><DIRECT>1</DIRECT><OUTMOD>2</OUTMOD><IDOCTYP>ZSDM_STAGCOLLTEMA</IDOCTYP><MESTYP>ZSDM_STAGCOLLTEMA</MESTYP><STDMES>ZSDM_S</STDMES><SNDPOR>SAPAFJ</SNDPOR><SNDPRT>LS</SNDPRT><SNDPRN>AFJ201</SNDPRN><RCVPOR>A000000009</RCVPOR><RCVPRT>LS</RCVPRT><RCVPRN>NWD207</RCVPRN><CREDAT>20050921</CREDAT><CRETIM>114637</CRETIM><SERIAL>20050921105748</SERIAL></EDI_DC40><Z1SDM_STAG SEGMENT="1"><MSGFN>004</MSGFN><J_3ASEAN>06A</J_3ASEAN><Z1SDM_COLL SEGMENT="1"><MSGFN>004</MSGFN><_-AFS_-COLLECTION>3MKA</_-AFS_-COLLECTION><SPART>02</SPART><ZUSCITA>U</ZUSCITA><ZLINEA>B</ZLINEA><ZSTAGIONALITA>1</ZSTAGIONALITA><Z1SDM_AZIONEVEND SEGMENT="1"><MSGFN>004</MSGFN><ZAZIONEVEND>261VV</ZAZIONEVEND></Z1SDM_AZIONEVEND><Z1SDM_AZIONEVEND SEGMENT="1"><MSGFN>004</MSGFN><ZAZIONEVEND>661</ZAZIONEVEND><ZCAMPIONARIO>B</ZCAMPIONARIO></Z1SDM_AZIONEVEND><Z1SDM_TEMI SEGMENT="1"><MSGFN>004</MSGFN><TEXT>Sisley Mktg Base AI</TEXT><TEXT1>A/I 2006</TEXT1><Z1SDM_PERIODI SEGMENT="1"><J_4AKNUMH>2343453464</J_4AKNUMH></Z1SDM_PERIODI><Z1SDM_PERIODI SEGMENT="1"><J_4AKNUMH>2343453454</J_4AKNUMH></Z1SDM_PERIODI></Z1SDM_TEMI></Z1SDM_COLL></Z1SDM_STAG></IDOC></ZSDM_STAGCOLLTEMA>

Thanks

Former Member
0 Kudos

Do also let me know the expected output for this input.

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Alessandro Berta,

We had the same scenario in our project, my suggestion is

[1]first form the hierarchical structure with Fixed width fields [Use padding functions]

[2]Use the following code to extract field values from a hierarchical structure and form flat row structure [Use java mapping]

[3]Use content conversion in File adapter to remove XML tags in Flat structure.

JAVA Mapping Code:



import java.util.regex.Pattern;
import com.sap.aii.mapping.api.StreamTransformation;


/**
 * @author Ananth Chinnaraj
 *
 */

public class FlatStructureConverter implements StreamTransformation{

		private Map map;
		
		/** 
		 *  method setParamters is required, but we do not do anything with it 
		 * */
		public void setParameter(Map param){
			map = param;
		} 
	
		/** 
		 * method execute is called by the XI mapping program 
		 * */
		public void execute(InputStream inStream, OutputStream out){
	
			try{
				PrintWriter prt = new PrintWriter(out,false);
				BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
				String str;
				String s[]=null;

				//Specify your Hier record name here
				Pattern pattern = Pattern.compile("\<HIER_RECORD\>");
				
				//Create XML tags
				prt.print("<?xml version= "1.0"?>");
				prt.print("<"+MSGTYPE+" xmlns:ns=""+NAMESPACE+"">");
				
				while ((str = in.readLine()) != null) {
					//form string array for each Hier Record
					s = pattern.split(str);
					String row=null;
					
					for(int i=0;i<s.length;i++){
						//Remove XML tags for Fields in Hier strutcure
						row = s<i>.replaceAll("\<.*?\>","");
												
						
						prt.print("<Row><RowValue>");
						prt.print(row);
						prt.print("</RowValue></Row>");
					}
				}
				prt.print("</"+MSGTYPE+">");
				prt.flush();
			}
			catch(Exception e){
				e.printStackTrace();
			}
	
		}
		
		
	  
	

}

I hope this helps,

Regards,

Ananth

Former Member
0 Kudos

Hi Alessandro,

You can have the flattening of this tree like structure by Content Conversion in Receiver File Adapter, Please go through this blog by Mr. Arpit Seth:

/people/arpit.seth/blog/2005/06/02/file-receiver-with-content-conversion

Also,

http://help.sap.com/saphelp_nw04/helpdata/en/d2/bab440c97f3716e10000000a155106/content.htm

I hope if it helps, you'll give me points

Thanks & Regards,

Varun Joshi

Former Member
0 Kudos

Thanks, but i need to do the conversion with mapping, not at the adapter level.

thanks anyway