in one of our requirement we are receiving a string of n character and at provider at we we sending that to SAP. Due to some limitation at target end, we need to check for string that if its more then 100 char, we need to split that and send to target application in 2 different segment(same name) like
input - This is a test message......(till 150 char)
in XSLT transformation -we need to split it like
<text>first 100 char<text>
<text> 101 to 200 char<text>
...
Since number of character is not predefined so I cant use substring function here. This should be as a part of loop..
Could someone pls help here.
In XSLT 2.0, you could do something like this:
Example simplified to splitting the input into tokens of (up to) 6 characters each.
XML
<input>abcde1abcde2abcde3abcde4abcde5abcde6abcde7abcde8abc</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:variable name="string" select="input" />
<xsl:for-each select="0 to (string-length($string) - 1) idiv 6">
<token>
<xsl:value-of select="substring($string, . * 6 + 1, 6)" />
</token>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Result
<output>
<token>abcde1</token>
<token>abcde2</token>
<token>abcde3</token>
<token>abcde4</token>
<token>abcde5</token>
<token>abcde6</token>
<token>abcde7</token>
<token>abcde8</token>
<token>abc</token>
</output>
Here's an option for 1.0 (works in 2.0 too). It uses a recursive template call.
XML Input (Thanks Michael)
<input>abcde1abcde2abcde3abcde4abcde5abcde6abcde7abcde8abc</input>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<output>
<xsl:call-template name="tokenize">
<xsl:with-param name="input" select="."/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="input"/>
<xsl:param name="length" select="6"/>
<token><xsl:value-of select="substring($input,1,$length)"/></token>
<xsl:if test="substring($input,$length+1)">
<xsl:call-template name="tokenize">
<xsl:with-param name="input" select="substring($input,$length+1)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
XML Output
<output>
<token>abcde1</token>
<token>abcde2</token>
<token>abcde3</token>
<token>abcde4</token>
<token>abcde5</token>
<token>abcde6</token>
<token>abcde7</token>
<token>abcde8</token>
<token>abc</token>
</output>