1

Im having data in XML has this

1 $ Mahesh $ SE $ ITO;2 $ umesh $ TE $ HBS;3 $ somesh$ SE $ ITO;

I have to split this string based on ';' and then each split string with '$'

I could able to do one level, but got strucked with next level of spplit

<xsl:template match="text()" name="split">
    <xsl:param name="pText" select="."/>
    <xsl:if test="string-length($pText)">
        <xsl:if test="not($pText=.)">
            <br />
        </xsl:if>
        <xsl:value-of select="substring-before(concat($pText,';'),';')"/>


        <xsl:call-template name="split">
            <xsl:with-param name="pText" select="substring-after($pText, ';')"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

2
  • 2
    Possible duplicate and stackoverflow.com/search?q=[xslt]+split Commented Jun 22, 2011 at 14:39
  • 1
    Can u please tell me how to increase probability , i marked all question s as answered, not sure what exactly ur expecting Commented Jun 26, 2011 at 12:41

1 Answer 1

2

Use this template:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="s">1 $ Mahesh $ SE $ ITO;2 $ umesh $ TE $ HBS;3 $ somesh$ SE $ ITO;</xsl:variable>

        <result>
            <xsl:call-template name="split">
                <xsl:with-param name="pText" select="$s"/>
            </xsl:call-template>
        </result>
    </xsl:template>


    <xsl:template name="split">
        <xsl:param name="pText"/>

        <xsl:if test="string-length($pText)">

            <group value="{substring-before($pText, ';')}">
                <xsl:call-template name="split2">
                    <xsl:with-param name="pText" select="substring-before($pText, ';')"/>
                </xsl:call-template>
            </group>

            <xsl:call-template name="split">
                <xsl:with-param name="pText" select="substring-after($pText, ';')"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="split2">
        <xsl:param name="pText"/>

        <xsl:if test="string-length($pText)">
            <item>
                <xsl:choose>
                    <xsl:when test="contains($pText, '$')">
                        <xsl:value-of select="normalize-space(substring-before($pText, '$'))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space($pText)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </item>

            <xsl:call-template name="split2">
                <xsl:with-param name="pText" select="substring-after($pText, '$')"/>
            </xsl:call-template>
        </xsl:if>

    </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="utf-8"?>
<result>
  <group value="1 $ Mahesh $ SE $ ITO">
    <item>1</item>
    <item>Mahesh</item>
    <item>SE</item>
    <item>ITO</item>
  </group>
  <group value="2 $ umesh $ TE $ HBS">
    <item>2</item>
    <item>umesh</item>
    <item>TE</item>
    <item>HBS</item>
  </group>
  <group value="3 $ somesh$ SE $ ITO">
    <item>3</item>
    <item>somesh</item>
    <item>SE</item>
    <item>ITO</item>
  </group>
</result>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.