Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2008, 11:59 AM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default If field not present then add spaces and concat

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

 
Old June 4th, 2008, 12:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old June 4th, 2008, 12:18 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>

 
Old June 4th, 2008, 12:20 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default


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>
 
Old June 4th, 2008, 01:40 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old June 4th, 2008, 01:51 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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+ZS ERIALNO>
</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

 
Old June 4th, 2008, 02:40 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old June 4th, 2008, 03:27 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old June 4th, 2008, 04:00 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old June 4th, 2008, 04:02 PM
Authorized User
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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”









Similar Threads
Thread Thread Starter Forum Replies Last Post
Concat autonumber field with a constant character sathija Access 5 July 6th, 2007 07:25 AM
add a new row when cursor comes to the last field bullsb Classic ASP Professional 8 March 11th, 2007 04:11 PM
add a new row when cursor comes to the last field bullsb Javascript How-To 2 February 26th, 2007 11:26 PM
add a custom field to a mail item [email protected] Visual Studio 2005 0 May 15th, 2006 10:25 AM
Add field to top of recordset vertigo Beginning VB 6 6 July 14th, 2004 03:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.