Field Name | Fixed Length (in bytes) | |
1 | Number | 8 |
2 | Type | 2 |
3 | Date | 8 |
4 | Text | 30 |
5 | PIC | 10 |
6 | Location | 6 |
<Table>
<Record>
<Number>00000001</Number>
<Type>A</Type>
<Date>20230701</Date>
<Text>あいうえお</Text>
<PIC>Tim</PIC>
<Location>123456</Location>
</Record>
<Record>
<Number>00000002</Number>
<Type>B</Type>
<Date>20230701</Date>
<Text>漢字-FullWidth</Text>
<PIC>Lisa</PIC>
<Location>123456</Location>
</Record>
<Record>
<Number>00000003</Number>
<Type>C</Type>
<Date>20230701</Date>
<Text>FullWidthひらがな</Text>
<PIC>Mike</PIC>
<Location>123456</Location>
</Record>
:
</Table>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Record">
<xsl:apply-templates />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="Number">
<xsl:value-of
select="substring(concat(., ' '), 1, 8)"/>
</xsl:template>
<xsl:template match="Type">
<xsl:value-of
select="substring(concat(., ' '), 1, 2)"/>
</xsl:template>
<xsl:template match="Date">
<xsl:value-of
select="substring(concat(., ' '), 1, 8)"/>
</xsl:template>
<xsl:template match="Text">
<xsl:value-of
select="substring(concat(., ' '), 1, 30)"/>
</xsl:template>
<xsl:template match="PIC">
<xsl:value-of
select="substring(concat(., ' '), 1, 10)"/>
</xsl:template>
<xsl:template match="Location">
<xsl:value-of
select="substring(concat(., ' '), 1, 6)"/>
</xsl:template>
</xsl:stylesheet>
Shift-JIS
<?xml version="1.0" encoding="Shift-JIS"?>
MS932
<?xml version="1.0" encoding="MS932"?>
Shift-JIS
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.lang.Math;
def Message processData(Message message) {
//take a message body, properties, headers
def body = message.getBody(java.lang.String) as String;
def properties = message.getProperties();
def headers = message.getHeaders();
//Get the number of records
String record_name = headers.get("RecordName")
String[] lines_record = body.split("<"+record_name+">", -1);
int num_records = lines_record.length;
//Get the number of fields
int num_fields = Integer.parseInt(headers.get("NumberOfFields"));
//you will send this new message body
def new_body = new StringBuffer();
//iterate over records
for(int r=1; r<num_records; r++){
//iterate over fields
for(int f=1; f<=num_fields; f++){
//take the field value
String field_name = properties.get("field"+String.valueOf(f));
String[] lines1 = lines_record[r].split("<"+field_name+">", -1);
String[] lines2 = lines1[1].split("</"+field_name+">", 2);
String field_value = lines2[0];
//take the fixed length for the field
int fixed_length = Integer.parseInt(properties.get("length"+String.valueOf(f)));
//you will keep the padded field value here
String padded;
//calculate the number of spaces to pad
//you need to choose a right character code for your language. Ex) Japanese -> Shift_JIS
int bytes = field_value.getBytes("Shift_JIS").length;
int num_to_pad = fixed_length - bytes;
//if padding is necessary for the fixed length
if (num_to_pad > 0){
//pad the field value with spaces
padded = field_value + new String(new char[num_to_pad]).replace('\0', ' ');
}
//case when bytes of the field value exceeds the fixed legnth
else {
//take current bytes and a current length of the field_value
int cur_bytes = bytes;
int cur_len = field_value.length();
//trim the last exceeding characters one by one
while (cur_bytes > fixed_length){
cur_len = field_value.length();
field_value = field_value.substring(0, cur_len-1);
cur_bytes = field_value.getBytes("Shift_JIS").length;
}
//if the last trimmed character was full-width, you need to pad it with one more space
if ( cur_bytes != fixed_length)
field_value = field_value + new String(new char[1]).replace('\0', ' ');
//set in the padded
padded = field_value;
}
//append in the new_body
new_body.append(padded);
}
//append a line break (CR/LF)
new_body.append('\r\n');
}
//set the new body back to the message body
message.setBody(new_body);
return message;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
9 | |
9 | |
6 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 |