Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to apply space between data in xslt

Tags:

xslt

xslt-1.0

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

like image 800
pubby Avatar asked May 25 '11 11:05

pubby


People also ask

How do you add a space in xsl?

With Apache FOP, in order to output a space like &nbsp; in HTML you would have to insert &#160; . &#160; always worked for me, but you can also try with &#x00A0; to see if it works. <fo:leader> without any attributes will generate an empty line that will fit the whole page width, generating an empty line.

What is xsl Strip space?

The <xsl:strip-space> element strips white-space-only text nodes in the specified elements. By default, all white-space-only text nodes are preserved. If an element name matches a name test in an <xsl:strip-space> element, it is removed from the set of white-space-preserving element names.

What is the use of namespace in XSLT?

In XML a namespace is a collection of names used for elements and attributes. A URI (usually, a URL) is used to identify a particular collection of names.

What is text () in XSLT?

The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


2 Answers

Try using

<xsl:text>        </xsl:text>

The space is between those tags.

For more info: XSLT Controlling Whitespace

like image 55
Danpe Avatar answered Oct 02 '22 20:10

Danpe


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                            "
like image 37
Dimitre Novatchev Avatar answered Oct 02 '22 21:10

Dimitre Novatchev