[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: XSLT for CSV to XML Conversion
On 15.02.2019 18:15, Rahul Singh rahulsinghindia15@xxxxxxxxx wrote:
I wrote XSL code for CSV to XML conversion but i am not getting proper output as for #Id 3 in XML. and, Also I need output as per given expected output below. *Expected Output:* It is not clear why you do not simply use the XPath 2 and later "tokenize" function to break up your text into record or row lines and each record or row into columns. Here is a suggestion to to it in XSLT 3 (supported by Saxon 9.8 or later): <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" version="3.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="root"> <xsl:copy> <xsl:variable name="lines" as="xs:string*" select="tokenize(., ' ')"/> <xsl:variable name="cols" select="tokenize(head($lines), '\|')"/> <xsl:apply-templates select="tail($lines)" mode="row"> <xsl:with-param name="cols" select="$cols"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match=".[. instance of xs:string]" mode="row"> <xsl:param name="cols"/> <row> <xsl:for-each select="tokenize(., '\|')"> <xsl:element name="{let $p := position() return $cols[$p]}" expand-text="yes">{.}</xsl:element> </xsl:for-each> </row> </xsl:template> </xsl:stylesheet> Online sample at https://xsltfiddle.liberty-development.net/ncdD7m6
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|