replace substrings using lookup table with xslt - xslt

I have a few strings containing a variant of Hexadecimal strings (source is framemaker if one would care). Strings could therefore look like
this is some sentence with some hex code\x27 s , and we need that
fixed.
and will need to be changed to
this is some sentence with some hex code's , and we need that fixed.
In reality there can be a few of these in a single string, so I'm looking on the best way to walk through the text, capture all hex codes (looking like \x## ) and replace all of these codes with the correct character. I have made a xml list / lookup table containing all the characters as follows :
<xsl:param name="reflist">
<Code Value="\x27">'</Code>
<Code Value="\x28">(</Code>
<Code Value="\x29">)</Code>
<Code Value="\x2a">*</Code>
<Code Value="\x2b">+</Code>
<!-- much more like these... -->
</xsl:param>
For now I used a simple replace argument but there are simply too many characters to make this workable.
What's the best way to do this?

One can completely avoid using any "reference table" -- like this:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my:my" exclude-result-prefixes="my xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[matches(., '\\x(\d|[a-f])+')]">
<xsl:analyze-string select="." regex="\\x(\d|[a-f])+" >
<xsl:matching-substring>
<xsl:value-of select=
"codepoints-to-string(my:hex2dec(substring(.,3), 0))"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:function name="my:hex2dec" as="xs:integer">
<xsl:param name="pStr" as="xs:string"/>
<xsl:param name="pAccum" as="xs:integer"/>
<xsl:sequence select=
"if(not($pStr))
then $pAccum
else
for $char in substring($pStr, 1, 1),
$code in
if($char ge '0' and $char le '9')
then xs:integer($char)
else
string-to-codepoints($char) - string-to-codepoints('a') +10
return
my:hex2dec(substring($pStr,2), 16*$pAccum + $code)
"/>
</xsl:function>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<t>
<p>this is some sentence with some hex code\x27 s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x28 s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x29 s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2a s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2b s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2c s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2d s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2e s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2f s ,
and we need that fixed.</p>
</t>
the wanted, correct result is produced:
<t>
<p>this is some sentence with some hex code' s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code( s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code) s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code* s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code+ s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code, s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code- s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code. s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code/ s ,
and we need that fixed.</p>
</t>
Do note:
This transformation is generic and can correctly process any hexadecimal unicode code.
For example, if the same transformation is applied on this XML document:
<t>
<p>this is some sentence with some hex code\x0428\x0438\x0448 s ,
and we need that fixed.</p>
</t>
the correct result (containing the Bulgarian word for "grill" in Cyrillic) is produced:
<t>
<p>this is some sentence with some hex codeШиш s ,
and we need that fixed.</p>
</t>

Use analyze-string as in
<xsl:template match="text()">
<xsl:analyze-string select="." regex="\\x[0-9a-f]{{2}}" flags="i">
<xsl:matching-substring>
<xsl:value-of select="$reflist/Code[#Value = .]"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
I would also suggest to use a key e.g.
<xsl:param name="reflist" as="document-node()">
<xsl:document>
<Root>
<Code Value="\x27">'</Code>
<Code Value="\x28">(</Code>
<Code Value="\x29">)</Code>
<Code Value="\x2a">*</Code>
<Code Value="\x2b">+</Code>
<!-- much more like these... -->
</Root>
</xsl:document>
</xsl:param>
<xsl:key name="code-by-value" match="Code" use="#Value"/>
then the lookup can be improved to
<xsl:template match="text/text()">
<xsl:analyze-string select="." regex="\\x[0-9a-f]{{2}}" flags="i">
<xsl:matching-substring>
<xsl:value-of select="key('code-by-value', ., $reflist)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
I have found some time to morph the suggestions made into working code, with the input being
<root>
<text>this is some sentence with some hex code\x27 s , and we need that \x28and this\x29 fixed.</text>
</root>
and the complete stylesheet being
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:param name="reflist" as="document-node()">
<xsl:document>
<Root>
<Code Value="\x27">'</Code>
<Code Value="\x28">(</Code>
<Code Value="\x29">)</Code>
<Code Value="\x2a">*</Code>
<Code Value="\x2b">+</Code>
<!-- much more like these... -->
</Root>
</xsl:document>
</xsl:param>
<xsl:key name="code-by-value" match="Code" use="#Value"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()">
<xsl:analyze-string select="." regex="\\x[0-9a-f]{{2}}" flags="i">
<xsl:matching-substring>
<xsl:value-of select="key('code-by-value', ., $reflist)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
Saxon 9.4 transforms the input as follows:
<root>
<text>this is some sentence with some hex code' s , and we need that (and this) fixed.</text>
</root>

Related

Getting value in between keys using XSLT

I Need to get the value in the XML file in between each key. For example, I have a list of keys to be used, and each key there's a corresponding output element. The keys can be placed anywhere, there is no proper order in where the key is needed to place. I need to do this in XSLT 2.0, and I don't have any idea on how will I do this.
Keys: Element:
/OPDH/ - ROOT/ELEMENT1/ABCD
/EKPH/ - ROOT/ELEMENT2/POIU
/SGDE/ - ROOT/ELEMENT3/WXYZ
...some other keys...
NOTE: Keys: is in BOLD, and Element is in ITALIC BOLD.
If I have a sample input like this:
1.)
<DATA>/OPDH/FLOWING SOLUTION/SGDE/Number0983713/EKPH/Sample test/some other keys/</DATA>
OR it can be:
2.)
<DATA>/some other keys/afdsf/SGDE/Number0983713/some other keys/PIHSAGA/OPDH/FLOWING SOLUTION/some other keys/No exception/EKPH/Sample test/some other keys/</DATA>
The expected output should look like this:
1.
<ROOT>
<ELEMENT1>
<ABCD>FLOWING SOLUTION</ABCD>
</ELEMENT1>
<ELEMENT2>
<POIU>Sample test</POIU>
</ELEMENT2>
<ELEMENT3>
<SGDE>Number0983713</SGDE>
</ELEMENT3>
...some other keys...
</ROOT>
2.
<ROOT>
...some other keys...
<ELEMENT3>
<SGDE>Number0983713</SGDE>
</ELEMENT3>
...some other keys...
<ELEMENT1>
<ABCD>FLOWING SOLUTION</ABCD>
</ELEMENT1>
...some other keys...
<ELEMENT2>
<POIU>Sample test</POIU>
</ELEMENT2>
...some other keys...
</ROOT>
Thank you.
Here is a partial suggestion that uses analyze-string:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf" version="2.0">
<xsl:param name="keys">
<element key="/OPDH/">ROOT/ELEMENT1/ABCD</element>
<element key="/EKPH/">ROOT/ELEMENT2/POIU</element>
<element key="/SGDE/">ROOT/ELEMENT3/WXYZ</element>
<element key="/some other keys/">ROOT/FOO/BAR</element>
</xsl:param>
<xsl:output indent="yes"/>
<xsl:variable name="pattern" as="xs:string"
select="concat('(', string-join($keys/element/#key, '|'), ')', '(.*?)', '(', string-join($keys/element/#key, '|'), ')')"/>
<xsl:key name="ref" match="element" use="#key"/>
<xsl:function name="mf:extract" as="element()*">
<xsl:param name="input" as="xs:string"/>
<xsl:analyze-string select="$input" regex="{$pattern}">
<xsl:matching-substring>
<xsl:if test="position() eq 1">
<element path="{key('ref', regex-group(1), $keys)}">
<xsl:value-of select="regex-group(2)"/>
</element>
<xsl:sequence
select="mf:extract(substring($input, string-length(concat(regex-group(1), regex-group(2))) + 1))"
/>
</xsl:if>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:template match="DATA">
<xsl:copy>
<xsl:sequence select="mf:extract(.)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This transforms the input
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<DATA>/OPDH/FLOWING SOLUTION/SGDE/Number0983713/EKPH/Sample test/some other keys/</DATA>
<DATA>/some other keys/afdsf/SGDE/Number0983713/some other keys/PIHSAGA/OPDH/FLOWING SOLUTION/some other keys/No exception/EKPH/Sample test/some other keys/</DATA>
</Root>
into list of elements with the extracted data and the path to build:
<DATA>
<element path="ROOT/ELEMENT1/ABCD">FLOWING SOLUTION</element>
<element path="ROOT/ELEMENT3/WXYZ">Number0983713</element>
<element path="ROOT/ELEMENT2/POIU">Sample test</element>
</DATA>
<DATA>
<element path="ROOT/FOO/BAR">afdsf</element>
<element path="ROOT/ELEMENT3/WXYZ">Number0983713</element>
<element path="ROOT/FOO/BAR">PIHSAGA</element>
<element path="ROOT/ELEMENT1/ABCD">FLOWING SOLUTION</element>
<element path="ROOT/FOO/BAR">No exception</element>
<element path="ROOT/ELEMENT2/POIU">Sample test</element>
</DATA>
I am not quite sure whether that is doing the right job as I am not sure what determines the order and contents of the two samples you have provided and what e.g. /some other keys/ is meant to express. Tell us whether the result has the data you want or clarify your question and the samples you have shown. It should be easy to generate the XML from the above intermediary results once we have established that the right data is extracted.
You wrote very little about keys, so I assume that:
Your input file contains both:
key list (in KEYS tag),
actual source (in DATA tag).
Both these tags are children of the source ROOT tag.
KEYS tag contains in each row a pair of key value and output path, where
respective content for this key shoud be placed.
Assume that your full input is:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<KEYS>
/OPDH/ - ROOT/ELEMENT1/ABCD
/EKPH/ - ROOT/ELEMENT2/POIU
/SGDE/ - ROOT/ELEMENT3/SGDE
</KEYS>
<DATA>/OPDH/FLOWING SOLUTION/SGDE/Number0983713/EKPH/Sample test/</DATA>
</ROOT>
Then you can write the XSLT as follows:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="ROOT">
<xsl:copy>
<!-- Divide KEYS into rows -->
<xsl:variable name="keys_1" select="tokenize(KEYS, '
')"/>
<!-- # of rows -->
<xsl:variable name="nn" select="count($keys_1)"/>
<!-- Drop 1st and last (empty) row -->
<xsl:variable name="keys" select="subsequence($keys_1, 2, $nn - 2)"/>
<!-- Divide DATA into tokens -->
<xsl:variable name="data_1" select="tokenize(DATA, '/')"/>
<!-- # of tokens -->
<xsl:variable name="nn" select="count($data_1)"/>
<!-- Drop 1st and last (empty) token -->
<xsl:variable name="data" select="subsequence($data_1, 2, $nn - 2)"/>
<!-- Generate output data for each row from keys -->
<xsl:for-each select="$keys">
<!-- Divide the keys row into tokens -->
<xsl:variable name="parts" select="tokenize(., '/')"/>
<!-- # of tokens -->
<xsl:variable name="nn" select="count($parts)"/>
<!-- Source key - token No 2 (after the 1st '/') -->
<xsl:variable name="srcKey" select="$parts[2]"/>
<!-- path - tokens after 'ROOT' -->
<xsl:variable name="path" select="subsequence($parts, 4)"/>
<!-- Open tags given in path -->
<xsl:for-each select="$path">
<xsl:text>
</xsl:text>
<!-- Spacing -->
<xsl:variable name="nn" select="position()"/>
<xsl:value-of select=
"string-join((for $i in 1 to $nn return ' '), '')"/>
<!-- Print opening tag -->
<xsl:value-of select="concat('<', ., '>')"
disable-output-escaping="yes"/>
</xsl:for-each>
<!-- Find position of the source key in data -->
<xsl:variable name="ind" select="index-of($data, $srcKey)[1]"/>
<!-- Get data from the next token -->
<xsl:value-of select="$data[$ind + 1]"/>
<!-- Close tags given in path -->
<xsl:for-each select="reverse($path)">
<xsl:variable name="nn" select="position()"/>
<!-- Spacing and NewLine - but not for the most inner tag -->
<xsl:if test="$nn > 1">
<xsl:text>
</xsl:text>
<xsl:value-of select=
"string-join((for $i in 1 to last() - $nn + 1 return ' '), '')"/>
</xsl:if>
<!-- Print closing tag -->
<xsl:value-of select="concat('</', ., '>')"
disable-output-escaping="yes"/>
</xsl:for-each>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>

XSLT - analyse following text value

I have a XML that text() node is not correctly formatted,
example:
<section>
<p>A number,of words have, been, suggested,as sources for,the term,</p>
</section>
Here after some ',' there are no space character and some does. what I need to do is if ',' not followed by a space character add a '*' character after the ',' character.
so, expected result,
<section>
<p>A number,*of words have, been, suggested,*as sources for,*the term*</p>
</section>
I think this can be done using regular expression but how can I select , characters that are not followed by space in regular expression in XSLT. also, some , exist just before the closing element (last , in the input) and I need to select those , as well.
<xsl:template match="para">
<xsl:copy>
<xsl:analyze-string select="." regex=",\s*">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<xsl:value-of select="'*'"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
You've replaced the last , in your input with ,* though your statement doesn't say that. I hope the below XSLT helps:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="p/text()">
<xsl:value-of select="replace(., ',([^\s]|$)',',*$1')"/>
</xsl:template>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#*, node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<section>
<p>A number,*of words have, been, suggested,*as sources for,*the term,*</p>
</section>
Here, the regex, ,([^\s]|$) matches the comma and the first character after that if not a space character; ,*$1 replaces the , with ,* and keeps the matched group intact.

XSL transform on text to XML with unparsed-text: need more depth

My rather well-formed input (I don't want to copy all data):
StartThing
Size Big
Colour Blue
coords 42, 42
foo bar
EndThing
StartThing
Size Small
Colour Red
coords 29, 51
machin bidule
EndThing
<!-- repeat a few thousand times-->
I have the below XSL which I modified from Parse text file with XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="text-encoding" as="xs:string" select="'iso-8859-1'"/>
<xsl:param name="text-uri" as="xs:string" select="'unparsed-text.txt'"/>
<xsl:template name="text2xml">
<xsl:variable name="text" select="unparsed-text($text-uri, $text-encoding)"/>
<xsl:analyze-string select="$text" regex="(Size|Colour|coords) (.+)">
<xsl:matching-substring>
<xsl:element name="{(regex-group(1))}">
<xsl:value-of select="(regex-group(2))"/>
</xsl:element>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="text2xml"/>
</xsl:template>
</xsl:stylesheet>
and it works fine on parsing the pairs into elements and values. It gives me this output:
<?xml version="1.0" encoding="UTF-8"?>
<Size>Big</Size>
<Colour>Blue</Colour>
<coords>42, 42</coords>
But I'd also like to wrap the values in the Thing tag so that my output looks like this:
<Thing>
<Size>Big</Size>
<Colour>Blue</Colour>
<coords>42, 42</coords>
</Thing>
One solution might be a regex that matches each group of lines after each "thing". Then matches substrings as I'm already doing. Or is there some other way to parse the tree?
I would use two nested analyze-string levels, an outer one to extract everything between StartThing and EndThing, and then an inner one that operates on the strings matched by the outer one.
<xsl:template name="text2xml">
<xsl:variable name="text" select="unparsed-text($text-uri, $text-encoding)"/>
<!-- flags="s" allows .*? to match across newlines -->
<xsl:analyze-string select="$text" regex="StartThing.*?EndThing" flags="s">
<xsl:matching-substring>
<Thing>
<!-- "." here is the matching substring from the outer regex -->
<xsl:analyze-string select="." regex="(Size|Colour|coords) (.+)">
<xsl:matching-substring>
<xsl:element name="{(regex-group(1))}">
<xsl:value-of select="(regex-group(2))"/>
</xsl:element>
</xsl:matching-substring>
</xsl:analyze-string>
</Thing>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>

XSLT to convert XML to text

I have a requirement where i need to generate a text file from XML document, generated text file should be in some particular format based on some rules. My XML looks something shown below:
<info>
<Tag1>
<Tag2>
<Tag3>
<PartNo>12 </PartNo>
</Tag3>
<DBOMInf1> 111 </DBOMInf1>
<DBOMInf2> sring </DBOMInf2>
</Tag2>
<Tag2>
<Tag3>
<PartNo>12 </PartNo>
</Tag3>
<DBOMInf1> 555 </DBOMInf1>
<DBOMInf2> abcd </DBOMInf2>
</Tag2>
</Tag1>
<Tag4>
<Tag5>
<Description>1200 liter </Description>
<No>12</No>
<Name>Engine</Name>
<Id>700</Id>
</Tag5>
</Tag4>
<action>
<actionId>700</actionId>
</action>
</info>
Expected output format in text:
ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2
700|12|Engine|1200 liter| 111|sring
700|12|Engine|1200 liter| 555|abcd
I am new to XSLT programming can any body share some info or example on how can i achieve this , i am familiar with basis of XSLT like templates matching, value of select.
Any link, or example will be very helpful.
Thanks
It looks like you want a row for each Tag2 element, in which case these are easily matching by doing the following (assuming you are currently positioned on the info element
<xsl:apply-templates select="Tag1/Tag2" />
But it also looks like you want to look up information from Tag5 elements. In this case you could use a key to look up such values, based on the No element. The key would be defined as follows:
<xsl:key name="Tags" match="Tag5" use="No" />
And to look up the tags for a given Tag2 element, you could do the following:
<xsl:apply-templates select="key('Tags', normalize-space(Tag3/PartNo))" />
(Note, normalize-space with remove the excess white-space from the element)
Here is the full XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:key name="Tags" match="Tag5" use="No" />
<xsl:template match="/info">
<xsl:text>ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2</xsl:text>
<xsl:value-of select="'
'" />
<xsl:apply-templates select="Tag1/Tag2" />
</xsl:template>
<xsl:template match="Tag2">
<xsl:apply-templates select="key('Tags', normalize-space(Tag3/PartNo))" />
<xsl:value-of select="concat(DBOMInf1, '|', DBOMInf2, '
')" />
</xsl:template>
<xsl:template match="Tag5">
<xsl:value-of select="concat(Id, '|', No, '|', Name, '|', Description, '|')" />
</xsl:template>
</xsl:stylesheet>
When applied to your input XML, the following text is output
ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2
700|12|Engine|1200 liter | 111 | sring
700|12|Engine|1200 liter | 555 | abcd

how to apply space between data in xslt

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,'
'),6), ',',substring-after(value,'
'))" />
</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 > 0 and string-length($text) > 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.