开发者

how to apply space between data in xslt

开发者 https://www.devze.com 2023-03-08 02:10 出处:网络
xml <block4> <tag> <name>50K</name> <value> 0501/045788775099 Praveen// name will come

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 SPAC开发者_JAVA百科E 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


Try using

<xsl:text>        </xsl:text>

The space is between those tags.

For more info: XSLT Controlling Whitespace


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                            "


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 ...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号