8

xml

      <block4>
          <tag>
            <name>50K</name>
            <value>
                0501/045788775099
                Praveen   // name will come 
                MENENDEZ Y PELAYOA CORUNA SPA // address will come
            </value>
         </tag>
      </block4>

i have written a xslt for this above tag but i have facing a problem with replacing remaining length with space the above value you can see in middle line praveen is there let us assume for this xml message praveen we recieved for another message we cam may be recieve Tom but maximum length is 35 so we need to caluclate the string name value remaining length we should replace with SPACE so i dunno how replace a space over there ...

xsl

<?xml version="1.0"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text" />   
 <xsl:template match="/">

      <xsl:for-each select ="block4/tag[name = '50K']">
 <xsl:value-of select="concat(substring(value, 1, 5), ',',substring(substring-         before(value,'&#13;'),6), ',',substring-after(value,'&#13;'))" />
  </xsl:for-each>
    </xsl:template>
  </xsl:stylesheet>

EXpected OUPUT lIKE:

0501/,045788775099,praveen............................MENENDEZ Y PELAYOA CORUNA SPA

where dots represents space dont assume dots

i need space over there assume think praveen is 7 char and remaining 28 char should make space wantedly in xslt

pubby
  • 173
  • 1
  • 3
  • 13

4 Answers4

19

Try using

<xsl:text>        </xsl:text>

The space is between those tags.

For more info: XSLT Controlling Whitespace

Danpe
  • 18,668
  • 21
  • 96
  • 131
  • i need space over there assume think praveen is 7 char and remaining 28 char should make space wantedly in xslt – pubby May 25 '11 at 11:10
4

let us assume for this xml message praveen we recieved for another message we cam may be recieve Tom but maximum length is 35 so we need to caluclate the string name value remaining length we should replace with SPACE so i dunno how replace a space over there ...

Use:

substring(concat($vstr, $vBlanks35), 1, 35)

This evaluates to the result of concatenating $vstr ('Praveen') with $vBlanks35 (35 blanks) and then taking the starting 35 characters.

Here is a complete example:

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

 <xsl:variable name="vstr" select="'Praveen'"/>

 <xsl:variable name="vBlanks35" select=
      "'                                   '"/>

 <xsl:template match="/">
     "<xsl:value-of select=
       "substring(concat($vstr, $vBlanks35), 1, 35)"/>"
 </xsl:template>

</xsl:stylesheet>

when this transformation is applied on any XML document (not used), the wanted, correct result is produced:

 "Praveen                            "
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • +1. I like it, even if I'm baffled by people trying to print formatted text using XSLT when they could do better things using HTML. But requirements are generally far from the ideal. – Emiliano Poggi May 25 '11 at 14:09
  • @empo: XSLT is a general PL -- can be used for anything. Jeni Tennison at the time created ascii-art with XSLT. I have a general LR-1 table-driven parser written in XSLT 2.0 and used it when I created JSON and XPath 2.0 parsers. – Dimitre Novatchev May 25 '11 at 14:33
  • yes I'm sure, more see I thing about it and more I realize that :-). – Emiliano Poggi May 25 '11 at 14:54
3

One (universal) way to add space in xml is to use the special xml attribute that preserves spaces:

<value xml:space="preserve"> 
        your 
        values 
        here ... 
</value>

Another method is to use XSL's preserve/strip space ...

veljkoz
  • 8,384
  • 8
  • 55
  • 91
  • @pubby - I'm sorry, but I don't understand what are you going for - maybe it would be best if you edited your question with example like: "This is my XSLT (and paste your current xslt). It produces output like this (and paste your current output). I want to have something like this: (and paste your desired output)." – veljkoz May 25 '11 at 11:27
  • @velijkoz the xslt which was i edited so far for that i can achive output like this 0501/,045788775099,praveenMENENDEZ Y PELAYOA CORUNA SPA but i need a space after praveen coz praveen is 7 char and about of 28 char of space in output then it should look like 0501/,045788775099,praveen MENENDEZ Y PELAYOA CORUNA SPA – pubby May 25 '11 at 11:36
1

You should use a XSLT version of SQL function RPAD:

<xsl:template name="rpad">
  <xsl:param name="text" />
  <xsl:param name="length" />
  <xsl:param name="char" select="' '" />
  <xsl:if test="$length &gt; 0 and string-length($text) &gt; 0">
    <xsl:value-of select="$text" />
    <xsl:call-template name="rpad">
      <xsl:with-param name="text" select="$char" />
      <xsl:with-param name="char" select="$char" />
      <xsl:with-param name="length" select="$length - string-length($text)" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Usage:

<xsl:call-template name="rpad">
  <xsl:with-param name="text" select="'your string here'" />
  <xsl:with-param name="length" select="35" />
</xsl:call-template>

Optionnally you may specify a char parameter for padding your string with a character other than space.

Erlock
  • 1,968
  • 10
  • 11