CSV to XML with modifications (XSLT?) -
i've been searching forums answer problem, without luck. i'm hoping can me. have simple csv file need convert xml (that part's easy), need modify contains sub elements. example:
what have:
<unit> <unitid>k000009107</unitid> <datelastmodified>2003-06-23</datelastmodified> <family>sapotaceae</family> <genus>pouteria</genus> <species>ferrugineo-tomentos</species> <identifier>smith, j</identifier> <startmonth>05</startmonth> <startyear>1997</startyear> <typestatus>type</typestatus> </unit>
what need:
<unit> <unitid>k000009107</unitid> <datelastmodified>2003-06-23</datelastmodified> <identification storedundername="true"> <family>sapotaceae</family> <genus>pouteria</genus> <species>ferrugineo-tomentos</species> <identifier>smith, j</identifier> <identificationdate> <startmonth>05</startmonth> <startyear>1997</startyear> </identificationdate> <typestatus>type</typestatus> </identification> </unit>
i need modification on large dataset. i'm guessing xslt job, can't figure out how work. ideas?
here 1 way of doing this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="/*"> <xsl:copy> <xsl:apply-templates select="unitid|datelastmodified"/> <identification storedundername="true"> <xsl:apply-templates select= "*[not(contains('|unitid|datelastmodified|startmonth|startyear|typestatus|', concat('|',name(),'|')))]"/> <identificationdate> <xsl:apply-templates select="startmonth|startyear"/> </identificationdate> <xsl:apply-templates select="typestatus"/> </identification> </xsl:copy> </xsl:template> </xsl:stylesheet>
when transformation applied on provided xml document:
<unit> <unitid>k000009107</unitid> <datelastmodified>2003-06-23</datelastmodified> <family>sapotaceae</family> <genus>pouteria</genus> <species>ferrugineo-tomentos</species> <identifier>smith, j</identifier> <startmonth>05</startmonth> <startyear>1997</startyear> <typestatus>type</typestatus> </unit>
the wanted, correct result produced:
<unit> <unitid>k000009107</unitid> <datelastmodified>2003-06-23</datelastmodified> <identification storedundername="true"> <family>sapotaceae</family> <genus>pouteria</genus> <species>ferrugineo-tomentos</species> <identifier>smith, j</identifier> <identificationdate> <startmonth>05</startmonth> <startyear>1997</startyear> </identificationdate> <typestatus>type</typestatus> </identification> </unit>
this transformation can shortened little bit @ expense of losing flexibility:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/*"> <xsl:copy> <xsl:copy-of select="unitid|datelastmodified"/> <identification storedundername="true"> <xsl:copy-of select= "*[not(contains('|unitid|datelastmodified|startmonth|startyear|typestatus|', concat('|',name(),'|')))]"/> <identificationdate> <xsl:copy-of select="startmonth|startyear"/> </identificationdate> <xsl:copy-of select="typestatus"/> </identification> </xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment