smandape Posted March 25, 2011 Posted March 25, 2011 Hey experts, I am facing a problem which may be real easy for you. The XML code is as follows <class_list> <classification id="GO:0000287" class_type="GO"> <category> Molecular Function </category> <description> magnesium ion binding </description> </classification> <classification id="GO:0004749" class_type="GO"> <category> Molecular Function </category> <description> ribose phosphate diphosphokinase activity </description> </classification> <classification id="GO:0009165" class_type="GO"> <category> Biological Process </category> <description> nucleotide biosynthetic process </description> </classification> </class_list> I want to extract the 'description data and @id as follows <Molecular_Function> <GO_Id> GO:0000287 </GO_Id> <description> magnesium ion binding </description> <GO_Id> GO:0004749 </GO_Id> <description> ribose phosphate diphosphokinase activity </description></Molecular_Function> <Biological Process> <GO_Id> GO:0009165 </GO_Id> <description> nucleotide biosynthetic process</description></Biological Process> can I use the code something as follows: <field name="GO_Biological_process"> <xsl:for-each select="class_list/classification[category='Biological Process']/description"> <xsl:value-of select="."/> </xsl:for-each> </field> I know something is wrong, but could you please help to solve this..i greatly appreciate your help.thank you,sammed
Martin Honnen Posted March 26, 2011 Posted March 26, 2011 It is a grouping problem that can be solved with XSLT 2.0 as follows: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"><xsl:output indent="yes"/><xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@*, node()"/> </xsl:copy></xsl:template><xsl:template match="class_list"> <xsl:copy> <xsl:for-each-group select="classification" group-by="normalize-space(category)"> <xsl:element name="{translate(current-grouping-key(), ' ', '_')}"> <xsl:apply-templates select="current-group()"/> </xsl:element> </xsl:for-each-group> </xsl:copy></xsl:template><xsl:template match="classification"> <xsl:apply-templates select="@id, description"/></xsl:template><xsl:template match="classification/@id"> <GO_Id><xsl:value-of select="."/></GO_Id></xsl:template></xsl:stylesheet>
Recommended Posts
Archived
This topic is now archived and is closed to further replies.