xml - xslt function to separate a string with delimiter -


i have string. want xslt function can separate every 2 characters of string delimiter '|'. e.g.:

input abadferewq

output ab|ad|fe|re|wq.

if you're using xslt 2.0, can use replace()...

xml input

<root>     <string>abadferewq</string> </root> 

xslt 2.0 (working example)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">   <xsl:output indent="yes"/>   <xsl:strip-space elements="*"/>    <xsl:template match="@*|node()">     <xsl:copy>       <xsl:apply-templates select="@*|node()"/>     </xsl:copy>   </xsl:template>    <xsl:template match="string">     <xsl:copy>       <!--       inner replace() adds '|' after every 2 characters.       outer replace() strips off trailing '|'.       -->       <xsl:value-of select="replace(                                 replace(                                     normalize-space(),                                     '(.{2})',                                     '$1|'),                                 '\|$',                                 '')"/>     </xsl:copy>   </xsl:template>  </xsl:stylesheet> 

output

<root>    <string>ab|ad|fe|re|wq</string> </root> 

if you're stuck using xslt 1.0, here's example close rudramuni tp's, simpler...

xslt 1.0 (working example)

<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="@*|node()">     <xsl:copy>       <xsl:apply-templates select="@*|node()"/>     </xsl:copy>   </xsl:template>    <xsl:template match="string">     <xsl:copy>       <xsl:call-template name="delimstring">         <xsl:with-param name="string" select="normalize-space()"/>       </xsl:call-template>     </xsl:copy>   </xsl:template>    <xsl:template name="delimstring">     <xsl:param name="string"/>     <xsl:variable name="remainder" select="substring($string,3)"/>     <xsl:value-of select="substring($string,1,2)"/>     <xsl:if test="$remainder">       <xsl:text>|</xsl:text>       <xsl:call-template name="delimstring">         <xsl:with-param name="string" select="$remainder"/>       </xsl:call-template>     </xsl:if>   </xsl:template>  </xsl:stylesheet> 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -