Topic
1 reply Latest Post - ‏2013-07-03T05:37:07Z by RamyaGullapalli
subratad
subratad
1 Post
ACCEPTED ANSWER

Pinned topic converting non-xml to xml using xslt 1.0

‏2011-02-10T19:04:05Z |
Hi,
I have a non-xml message like ARYXX 00002~TAG1~TAG2~Mark~10000~paul~20000~ . And I have to convert to an xml format

<body>
<name> Mark<name>
<salary>10000</salary>
<name>paul</name>
<salary>20000</salary>
</body>

How it can be done using xslt 1.0 0r 2.0 ?

Regards,
SD
  • RamyaGullapalli
    RamyaGullapalli
    2 Posts
    ACCEPTED ANSWER

    Re: converting non-xml to xml using xslt 1.0

    ‏2013-07-03T05:37:07Z  in response to subratad
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" exclude-result-prefixes="str" extension-element-prefixes="str"
        version="1.0">
        <xsl:template match="/">
            <xsl:variable name="string" select="/input"></xsl:variable>
            <xsl:variable name="pattern">~</xsl:variable>
            <xsl:call-template name="str:_split-pattern">
                <xsl:with-param name="string" select="$string" />
                <xsl:with-param name="pattern" select="$pattern" />
            </xsl:call-template>
        </xsl:template>
        <xsl:template name="str:_split-pattern">
            <xsl:param name="string"/>
            <xsl:param name="pattern"/>
            <xsl:choose>
                    <xsl:when test="contains($string, $pattern)">
                    <xsl:if test="not(starts-with($string, $pattern))">
                        <xsl:element name="token"><xsl:value-of select="substring-before($string, $pattern)" /></xsl:element>
                    </xsl:if>
                    <xsl:call-template name="str:_split-pattern">
                        <xsl:with-param name="string" select="substring-after($string, $pattern)" />
                        <xsl:with-param name="pattern" select="$pattern" />
                    </xsl:call-template>
                </xsl:when>
                </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>