pravz
pravz

Reputation: 235

How to split text by linefeed and concatenate with variable padding

hi here below i have shown my xml existing a value like three line feed but doubt how to fetch a value based on line feed in xslt logic can any help me

     <message>
    <block4>
         <tag>
    <name>57D</name>
    <value>BVALESM M0746A
           051028GB ES00069074
           6A051028 GBES00069</value>
         </tag>
    </block4>
</message>

here is my xslt which was i was trying but still some issues kindly please suggest me

<xsl:when test="tag[name = '57D'] ">
                    <xsl:variable name="l" select="substring-before(tag[name = '57D']/value, '&#13;')"/>
                    <xsl:variable name="r" select="substring-after(substring-before(tag[name = '57D']/value, '&#13;'), '&#13;')"/>


                    <xsl:value-of select="concat(substring(concat($l,'                                   '),1,35),substring(concat($r,'                                   '),1,35))"/>

                </xsl:when>

generating output as:

BVALESM M0746A 051028GB ES000690746A051028 GBES00069

it was considering total value after the first clrf so it was not checking logic

required output like

BVALESM M0746A                     051028GB ES00069074                6A051028 GBES00069

each line max has to be 35 , it was not every time data should come as 35 for each so if not means we need t insert space

Upvotes: 0

Views: 643

Answers (2)

Emiliano Poggi
Emiliano Poggi

Reputation: 24826

Ok, the actual question is heavily changed in the requirements since its original formulation. Now I'll provide a new answer.

The following transform behaves as follows:

  • Split lines by line-feed (using split-string template)
  • For each line, check 35 against the string length. If the string is lower than 35, padding is added to fill in the string till 35 (using padding template)
  • All lines are concatenated

[XSLT 1.0]

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="message/block4/tag">
        <xsl:variable name="result">
            <xsl:call-template name="split-string">
                <xsl:with-param name="string" select="value"/>
            </xsl:call-template>
        </xsl:variable>

        <xsl:value-of select="$result"/>

    </xsl:template>

    <xsl:template name="split-string">
        <xsl:param name="string"/> 
        <xsl:variable name="l" select="substring-before($string, '&#xA;')"/> 
        <xsl:variable name="r" select="substring-after($string, '&#xA;')"/>

        <xsl:choose>
            <xsl:when test="$l">
                <xsl:variable name="spaces">
                    <xsl:call-template name="padding">
                        <xsl:with-param name="times" 
                            select="35 - string-length(normalize-space($l))"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="concat(normalize-space($l),$spaces)" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="normalize-space($string)" />
            </xsl:otherwise>
        </xsl:choose>

        <xsl:if test="$r">
            <xsl:call-template name="split-string">
                <xsl:with-param name="string" select="$r" /> 
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="padding">
        <xsl:param name="times" select="0"/>
        <xsl:param name="spaces" select="''"/>
        <xsl:choose>
            <xsl:when   test="$times>0">
                <xsl:call-template name="padding">
                    <xsl:with-param name="times" select="$times - 1"/>
                    <xsl:with-param name="spaces" select="concat($spaces,' ')"/>    
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$spaces"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

When applied to the following input:

<message>
    <block4>
        <tag>
            <name>57D</name>
            <value>BVALESM M0746A
                051028GB ES00069074
                6A051028 GBES00069</value>
        </tag>
    </block4>
</message>

This output is produced:

BVALESM M0746A                     051028GB ES00069074                6A051028 GBES00069

Upvotes: 1

Petr Kozelka
Petr Kozelka

Reputation: 7990

there are some options but it heavily depends on your other constraints, mostly related to your xslt processor and its runtime environment:

  • can you use xslt 2.0, or need to stick with xslt 1.0 ?
  • exslt functions can be helpful: http://exslt.org/str/index.html
  • you can also process the data by recursively calling a named template on the data, and use xpath functions substring-before, substring-after - you will probably need also normalize-space to have constant whitespace separator - see http://www.w3.org/TR/xpath-functions/ for details

Upvotes: 0

Related Questions