Fixed FLAT File to XML -
need on xslt 2.0.
here request of fixed flat file using unparsed text , schema file having offset , length information using document reference file. details when start grouping not able go further...any appreciated move stuck mind.
source flat file
p01020 230039.... a30438 009090..... a30439 009039..... p02390 039438.... d03049 304830... a30493 304030... p30439 230300....
schema file:
<schema> <purchase recordidentifer="p"> <field1 length="5" offset="1" type="xs:string"/> <field2 length="6" offset="8" type="xs:string"/> </purchase> <account recordidentifer="a"> <field1 length="5" offset="1" type="xs:string"/> <field2 length="6" offset="8" type="xs:string"/> </account> <deposit recordidentifer="d"> <field1 length="5" offset="1" type="xs:string"/> <field2 length="6" offset="8" type="xs:string"/> </deposit>
xslt code
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:param name="text-url" select="'test2014032901.txt'"/> <xsl:param name="file1" select="document('schema.xml')"/> <xsl:output indent="yes"/> <xsl:template name="main"> <xsl:variable name="text" select="unparsed-text($text-url)"/> <xsl:variable name="lines" as="element(line)*"> <xsl:for-each select="tokenize($text,'\r?\n')[normalize-space()]"> <line> <xsl:value-of select="."/> </line> </xsl:for-each> </xsl:variable> <root> <xsl:for-each-group select="$lines" group-starting-with="line[starts-with(., 'p')]"> <purchases> <purchase> <xsl:for-each select="$file1/schema/purchase"> <xsl:for-each select="./*"> <xsl:element name="{name(.)}"> <xsl:value-of select="substring($lines, ./@offset,./@length)"/> </xsl:element> </xsl:for-each> </xsl:for-each> </purchase> </purchases> </xsl:for-each-group> <xsl:for-each-group select="$lines" group-starting-with="line[starts-with(., 'a')]"> <accounts> <account> <xsl:for-each select="$file1/schema/account"> <xsl:for-each select="./*"> <xsl:element name="{name(.)}"> <xsl:value-of select="substring($lines, ./@offset,./@length)"/> </xsl:element> </xsl:for-each> </xsl:for-each> </account> </accounts> </xsl:for-each-group> <xsl:for-each-group select="$lines" group-starting-with="line[starts-with(., 'd')]"> <deposits> <deposit> <xsl:for-each select="$file1/schema/deposit"> <xsl:for-each select="./*"> <xsl:element name="{name(.)}"> <xsl:value-of select="substring($lines, ./@offset,./@length)"/> </xsl:element> </xsl:for-each> </xsl:for-each> </deposit> </deposits> </xsl:for-each-group> </root> </xsl:template> </xsl:stylesheet>
output xml below
<root> <purchases> <purchase> <field1>01020</field1> <field2>230039</field2> </purchase> <purchase> <field1>02390</field1> <field2>039438</field2> </purchase> <purchase> <field1>30439</field1> <field2>230300</field2> </purchase> </purchases> <accounts> <account> <field1>30438</field1> <field2>009090</field2> </account> <account> <field1>01020</field1> <field2>230039</field2> </account> <account> <field1>01020</field1> <field2>230039</field2> </account> </accounts> <deposits> <deposit> <field1>03049</field1> <field2>304830</field2> </deposit> </deposits> </root>
any appreciated!!!.
thanks
i not sure understand use of for-each-group
, seems, @ least purchases, that
<purchases> <xsl:for-each select="$lines[starts-with(., 'p')]"> <purchase> <xsl:variable name="line" select="."/> <xsl:for-each select="$file1/schema/purchase/*"> <xsl:element name="{name(.)}"> <xsl:value-of select="substring($line, @offset, @length)"/> </xsl:element> </xsl:for-each> </purchase> </xsl:for-each> </purchases>
suffices give
<purchases> <purchase> <field1>p0102</field1> <field2>230039</field2> </purchase> <purchase> <field1>p0239</field1> <field2>039438</field2> </purchase> <purchase> <field1>p3043</field1> <field2>230300</field2> </purchase> </purchases>
Comments
Post a Comment