Hi,

I work with Scriptura Designer (eclipse based) for document generation.
I have to generate now a European transfer form. So I have to add a space between the characters.

ex: <chead:LastName>Van Messem</chead:LastName>
<chead:FirstName>Erwin</chead:FirstName>

So I want in return between each character a blanco space. (Like this: V a n M e s s e m)
How can I do that

Thanks in advance
greetz
Jan

Okay, here we go! Below is a transformation written in XSLT 1.0 that uses a call-template I wrote. This call-template will take ANY string value, string out all whitespace, lines feeds, tabs, spaces and return a string with spaces inserted after every non-whitespace character. If the string that was input into the call-template was a single character like 'a', then it will return just 'a' (with no space added). If the string entered into the call-template was ALL whitespace like ' ', it will return a empty string ''. (BTW in XSLT 2.0 this would be MUCH easier and shorter).

I have tested it with a fairly robust set of characters, but you may find there's some characters that do not behave properly. I'm not sure what kinds of strings you're going to be passing it.

For the following transformation sample, I used the following input document.

<chead:List xmlns:chead="http://somenamespace">
	<chead:Empty>     </chead:Empty>
	<chead:OneLength>!</chead:OneLength>
	<chead:LastName>Van Messem</chead:LastName>
	<chead:FirstName>Erwin</chead:FirstName>
	<chead:Garbage>a     		
	
			!!! ! 1! @#$^ @#$^@@$ % @ # $%!!+_</chead:Garbage>
	<!-- There are Tabs, Spaces and Returns in Garbage -->	
</chead:List>

Here's the sample transformation with the call-template.

<xsl:stylesheet version="1.0" xmlns:chead="http://somenamespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:template match="/">
		<xsl:element name="chead:Strings">
			<xsl:apply-templates select="chead:List/*" />
		</xsl:element>
	</xsl:template>

	<xsl:template match="chead:List/*" >
		<xsl:copy>			
			<xsl:call-template name="insertSpaces" >
				<xsl:with-param name="inputString" select="." />
			</xsl:call-template>
		</xsl:copy>
	</xsl:template>

	<xsl:template name="insertSpaces">
		<xsl:param name="inputString" select="''"/>
		<xsl:param name="stringposition" select="1"/>
		<xsl:param name="strippedinputString" select="translate($inputString,' &#x9;&#xA;&#xD;','')"/>
		<!--<xsl:param name="strippedinputString" select="translate($inputString,' ','')"/>-->
		<xsl:param name="originalStringLength" select="string-length($strippedinputString)"/>
		<xsl:param name="numberofSpaces" select="$originalStringLength - 1"/>
		<xsl:param name="finalStringLength" select="$originalStringLength + $numberofSpaces"/>
		<xsl:choose>
			<xsl:when test="$originalStringLength &lt; 2">
				<xsl:value-of select="$strippedinputString"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="firstpart" select="concat(substring($strippedinputString,1,$stringposition), ' ')"/>
				<xsl:variable name="secondpart" select="substring($strippedinputString,($stringposition + 1), string-length($strippedinputString))"/>
				<xsl:variable name="combinedString" select="concat($firstpart, $secondpart)"/>
				<xsl:choose>
					<xsl:when test="string-length($combinedString) = $finalStringLength">
						<xsl:value-of select="$combinedString"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="insertSpaces">
							<xsl:with-param name="strippedinputString" select="$combinedString"/>
							<xsl:with-param name="finalStringLength" select="$finalStringLength"/>
							<xsl:with-param name="originalStringLength" select="$originalStringLength"/>
							<xsl:with-param name="numberofSpaces" select="$numberofSpaces"/>
							<xsl:with-param name="stringposition" select="$stringposition + 2"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>

Here's the output of the transformation using the above input document.

<chead:Strings xmlns:chead="http://somenamespace">
	<chead:Empty></chead:Empty>
	<chead:OneLength>!</chead:OneLength>
	<chead:LastName>V a n M e s s e m</chead:LastName>
	<chead:FirstName>E r w i n</chead:FirstName>
	<chead:Garbage>a ! ! ! ! 1 ! @ # $ ^ @ # $ ^ @ @ $ % @ # $ % ! ! + _</chead:Garbage>
</chead:Strings>

Feel free to modify as it suits your needs. Without really detailed requirements and restrictions about the types of characters (or character sets) that are being used, it's hard to write the perfect solution. But that should get you started.

Edited 6 Years Ago by iceandrews: n/a

This article has been dead for over six months. Start a new discussion instead.