#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    XSL String manipulation


    Hi,

    I am a new to XSL programming. I have a requirement to add slash at every 4th character in a string. For example, a string value of 'abcdefghijklmn' to be translated to 'abcd/efgh/ijkl/mn'. Appreciate if someone help me out.

    Regards,

    Smitha
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    What will you be using to run the XSLT? For instance, "my web browser" or "PHP's XSLTProcessor".

    Comments on this post

    • xml-profi disagrees : help is needed
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    xml for test
    Code:
    <?xml version="1.0"?>
    <root>
    	<item>abcdefghijklm</item>
    	<item>12345678901234</item>
    </root>
    xsl rekursion
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output indent="yes" method="xml"/>
    	<xsl:template match="/">
    		<root>
    			<xsl:apply-templates select="root"/>
    		</root>
    	</xsl:template>
    	<xsl:template match="root">
    		<xsl:apply-templates select="item"/>
    	</xsl:template>
    	<xsl:template match="item">
    		<xsl:call-template name="allfour">
    			<xsl:with-param name="content" select="."/>
    		</xsl:call-template>
    	</xsl:template>
    	<xsl:template name="allfour">
    		<xsl:param name="content"/>
    		<xsl:param name="select" select="''"/>
    		<xsl:choose>
    			<xsl:when test="string-length($content) &gt;4">
    				<xsl:call-template name="allfour">
    					<xsl:with-param name="content" select="substring($content,5)"/>
    					<xsl:with-param name="select" select="concat($select,substring($content,1,4),'/')"/>
    				</xsl:call-template>
    			</xsl:when>
    			<xsl:otherwise>
    				<result>
    					<xsl:value-of select="concat($select,substring($content,1))"/>
    				</result>
    			</xsl:otherwise>
    		</xsl:choose>
    	</xsl:template>
    </xsl:stylesheet>
    result
    Code:
    <?xml version='1.0' ?>
    <root>
      <result>abcd/efgh/ijkl/m</result>
      <result>1234/5678/9012/34</result>
    </root>
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage

IMN logo majestic logo threadwatch logo seochat tools logo