Needed XSLT for string split
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">
开发者_JAVA百科 <xsl:with-param name="pText" select="substring-after($pText, ';')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
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>
精彩评论