|
Subject:
|
If field not present then add spaces and concat
|
|
Posted By:
|
pixelheart
|
Post Date:
|
6/4/2008 11:59:31 AM
|
Hi I would like to request guru's help here. I am basic user and still learning XSLT. I appreciate your inputs
I have total 10 Fields and trying to concat all of them, by checking if the field not exists then add spaces to it
Concat Starts here <xsl:choose> <xsl:when 1=""> <xsl:value-of select="1"/> </xsl:when> <xsl:otherwise> <xsl:value-of select=" "/> (Spaces added here) </xsl:otherwise> </xsl:choose> + Next Concat with the same condition above , until like 10 fields
Appreciate any help in advance
|
|
Reply By:
|
mhkay
|
Reply Date:
|
6/4/2008 12:07:31 PM
|
It would be useful, I think, to see what your input looks like and what output you expect from it.
Michael Kay http://www.saxonica.com/ Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 12:18:47 PM
|
INPUT
<?xml version="1.0" encoding="UTF-8"?> <ZWMMBID2> <IDOC BEGIN="1"> <E1MBXYH SEGMENT="1"> <XABLN>4000006853</XABLN> <USNAM>MOHANPX</USNAM> <E1MBXYI SEGMENT="1"> <CHARG>0000002102</CHARG> <ERFMG>2.000</ERFMG> <ERFME>KG</ERFME> <GRUND>0000</GRUND> <DABRZ>00000000</DABRZ> <ZE2MBXY SEGMENT="1"> <ZINVADJID>081541656</ZINVADJID> <ZSITE>SG1</ZSITE> <Z1633CODE>100030012134</Z1633CODE> <ZDELTAQTY>-000000002.0000</ZDELTAQTY> <ZCONTAINER>DUMMY</ZCONTAINER> <ZSERIALNO>DUMMY</ZSERIALNO> </ZE2MBXY> </E1MBXYI> </E1MBXYH> </IDOC> </ZWMMBID2>
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 12:20:10 PM
|
The INVADJ Contains number of field concatenated from the above let's say <ZE2MBXY SEGMENT="1"> <ZINVADJID>081541656</ZINVADJID> <ZSITE>SG1</ZSITE> <Z1633CODE>100030012134</Z1633CODE> <ZDELTAQTY>-000000002.0000</ZDELTAQTY> <ZCONTAINER>DUMMY</ZCONTAINER> <ZSERIALNO>DUMMY</ZSERIALNO> </ZE2MBXY>
i have to make sure to meet the fixed length format
OUTPUT Something like this
<ELC> <SYSINF>INVADJS IA WMCS FCBREDA 0000584216P0000000351200804231710522.0</SYSINF> <INVOWN>B58</INVOWN> <INVADJ>000022275NH WMCS_SYS 400601APALSU2 B58X 000000010.0000 KANew Empty Pal. Staple</INVADJ> </ELC>
|
|
Reply By:
|
mhkay
|
Reply Date:
|
6/4/2008 1:40:12 PM
|
Sorry, I can't see any relationship at all between your input and your output. You have FCBREDA in the output, where does it come from?
Michael Kay http://www.saxonica.com/ Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 1:51:13 PM
|
OK, Let's forget about the SYSINV and INVOWN fields. I will concentrate on INVADJ
From the two segments below <E1MBXYI SEGMENT="1"> <CHARG>0000002102</CHARG> <ERFMG>2.000</ERFMG> <ERFME>KG</ERFME> <GRUND>0000</GRUND> <DABRZ>00000000</DABRZ> <ZE2MBXY SEGMENT="1"> <ZINVADJID>081541656</ZINVADJID> <ZSITE>SG1</ZSITE> <Z1633CODE>100030012134</Z1633CODE> <ZDELTAQTY>-000000002.0000</ZDELTAQTY> <ZCONTAINER>DUMMY</ZCONTAINER> <ZSERIALNO>DUMMY</ZSERIALNO> </ZE2MBXY> </E1MBXYI>
Here is the example Fields list suppose to be <ELC> <ZINVADJID+ZSITE+Z1633CODE+ZDELTAQTY+ZCONTAINER+ZSERIALNO> </ELC> <ELC> <INVADJ>081541656SG1100030012134DUMMYDUMMY</INVADJ> </ELC>
Let's say, any of the field is not coming, i need to insert the blank spaces to make it fixed length format. Same Applies for SYSINF & INVOWN as well. One solution works for all the fields to make it fixed length.
Hope i explained in a better way
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 2:40:25 PM
|
btw i to make sure the below rules while generating the fixed length format
For Character, i need to pad with Zeros with mandatory field length Example: Text string of length n. C format fields are left justified with trailing spaces. Example for C(5) is boat
For Integer Integer of length n.Example for I(7) is 7654321.I format fields are right justified with leading zeros.Example for I(3) is 034
Numeric Field Numeric field of length 1+a+1+b. N format fields are right justified with leading zeros and trailing zeros. The first character is either a sign (+,-) or a blank character which is equivalent to a +. Examples for N(6.4) are: 000432.4500, +000432.4500, -000023.0000
For Literal Text, i need to count the length and pad spaces at the end
Eg L(100) "12345678.......95 "
Appreciate any help
|
|
Reply By:
|
mhkay
|
Reply Date:
|
6/4/2008 3:27:11 PM
|
If you're using 2.0, you can write a function like this:
<xsl:function name="f:pad"> <xsl:param name="inputField"/> <xsl:param name="length"/> <xsl:value-of select="concat(substring($inputField, 1, $length), substring($lotsOfSpaces, 1, $length - string-length($input-field))"/> </xsl:function>
where lotsOfSpaces is a global variable containing a long string of spaces.
Then you can make a sequence of calls like this:
<xsl:value-of select="f:pad(ZINVADJIN, 10)"/> <xsl:value-of select="f:pad(XSITE, 5)"/>
etc.
If you're using 1.0, you can write a named template very similar to the above, and call it using xsl:call-template - it's just a bit more verbose.
For padding numeric fields to a fixed width, you can use the built-in format-number() function, for example format-number(ERFMG, '00000.000').
Michael Kay http://www.saxonica.com/ Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 4:00:49 PM
|
I have written this code
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <ELC> <INVADJ> <xsl:call-template name="tpad"> <xsl:with-param name="inputField"> <xsl:value-of select="ZWMMBID2/IDOC/E1MBXYH/E1MBXYI/ZE2MBXY/ZINVADJID"/> </xsl:with-param> <xsl:with-param name="length"> <xsl:value-of select="number(9)"/> </xsl:with-param> </xsl:call-template> </INVADJ> </ELC>
</xsl:template> <xsl:variable name="lotsOfSpaces"> <text> </text> </xsl:variable> <xsl:template name="tpad"> <xsl:param name="inputField"/> <xsl:param name="length"/> <xsl:value-of select="concat(substring($inputField, 1, $length),substring($lotsOfSpaces, 1, $length - string-length($inputField)))"/> </xsl:template> </xsl:stylesheet>
But i am not sure how to format for different types like CHAR, NUMERIC and Integer
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 4:02:21 PM
|
Is there anyway we can pass one more param as format type, define those rules i mentioned below Format Specifier Description and Examples C(n) Text string of length n. C format fields are left justified with trailing spaces.Example for C(4) is boat I(n) Integer of length n.Example for I(7) is 7654321.I format fields are right justified with leading zeros.Example for I(3) is 034 L(n) This is a literal text string of n. This means that the text specified in the field name is to be written into the message string. N(a.b) Numeric field of length 1+a+1+b.N format fields are right justified with leading zeros and trailing zeros. The first character is either a sign (+,-) or a blank character which is equivalent to a +.Examples for N(6.4) are: 000432.4500,+000432.4500, -000023.0000
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 5:02:13 PM
|
I declared LotsofSpaces as below and it seems not working <xsl:variable name="lotsOfSpaces"> <text> <text/>
OR JUST THE LINE TYPED WITH SPACE BAR, STILL NO LUCK
</xsl:variable>
I used striling-length, it's reading 0. i guess i need to pass as string or something. any help is appreciated
|
|
Reply By:
|
mhkay
|
Reply Date:
|
6/4/2008 5:29:16 PM
|
>I declared LotsofSpaces as below and it seems not working <xsl:variable name="lotsOfSpaces"> <text> <text/> </xsl:variable>
Whitespace text nodes in a stylesheet are stripped out. You want
<xsl:variable name="lotsOfSpaces" select="' ... '"/>
Also, don't do
<xsl:with-param name="x"> <xsl:value-of select="y"/> </xsl:with-param>
XSLT doesn't have to be as verbose as that! Instead write
<xsl:with-param name="x" select="y"/>
It's a lot faster as well.
Michael Kay http://www.saxonica.com/ Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
Reply By:
|
mhkay
|
Reply Date:
|
6/4/2008 5:46:31 PM
|
> Is there anyway we can pass one more param as format type
Yes of course. You can pass any parameters that you like. Or you can have multiple functions/templates for the different types/formats.
But I'm getting the impression that you're thinking aloud about your program design: if you have a specific question or problem about XSLT, please ask it. We're not here to design or write your code for you.
Michael Kay http://www.saxonica.com/ Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
Reply By:
|
pixelheart
|
Reply Date:
|
6/4/2008 5:53:57 PM
|
Np, no designes involved. I tried to pass one more param for format as well, i am still getting errors. That's just to make it generic template. It just popped up in my mind, but nothing more.
i owe you a big one, not sure how can i payback. But it really helped me to output something i would like to see. Thanks for your help
I promise will continue only with the problems and not the designs. Anyways i am not that kind of expert in my opinion, As you can see my idiot coding above
Once again thanks a bunch, will come back later with more questions, not that i am not working to learn at all. I am breaking my head all the time.
|
|
Reply By:
|
mhkay
|
Reply Date:
|
6/5/2008 2:13:19 AM
|
>i am still getting errors
Then PLEASE, show us what you did and show us the errors. That's a much better way of making progress than to ask us to solve the problem from first principles.
Michael Kay http://www.saxonica.com/ Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|