Jump to content

Extracting elements in reverse order when shown


Recommended Posts

Hello Experts,I am trying to extract data from XML file which is as follows

<lineage>            <taxon scientificName="Homo" taxId="9605" rank="genus"/>            <taxon scientificName="Homininae" taxId="207598" rank="subfamily" hidden="true"/>            <taxon scientificName="Hominidae" taxId="9604" rank="family"/>            <taxon scientificName="Hominoidea" taxId="314295" rank="superfamily" hidden="true"/>            <taxon scientificName="Catarrhini" taxId="9526" rank="parvorder"/>            <taxon scientificName="Simiiformes" taxId="314293" rank="infraorder" hidden="true"/>            <taxon scientificName="Haplorrhini" taxId="376913" rank="suborder"/>            <taxon scientificName="Primates" taxId="9443" rank="order"/>            <taxon scientificName="Euarchontoglires" taxId="314146" rank="superorder"/>            <taxon scientificName="Eutheria" commonName="placentals" taxId="9347"/>            <taxon scientificName="Theria" taxId="32525" hidden="true"/>            <taxon scientificName="Mammalia" commonName="mammals" taxId="40674" rank="class"/>            <taxon scientificName="Amniota" commonName="amniotes" taxId="32524" hidden="true"/>            <taxon scientificName="Tetrapoda" commonName="tetrapods" taxId="32523" hidden="true"/>            <taxon scientificName="Sarcopterygii" taxId="8287" hidden="true"/>            <taxon scientificName="Euteleostomi" commonName="bony vertebrates" taxId="117571"/>            <taxon scientificName="Teleostomi" taxId="117570" hidden="true"/>            <taxon scientificName="Gnathostomata" commonName="jawed vertebrates" taxId="7776" rank="superclass" hidden="true"/>            <taxon scientificName="Vertebrata" commonName="vertebrates" taxId="7742"/>            <taxon scientificName="Craniata" taxId="89593" rank="subphylum"/>            <taxon scientificName="Chordata" commonName="chordates" taxId="7711" rank="phylum"/>            <taxon scientificName="Deuterostomia" taxId="33511" hidden="true"/>            <taxon scientificName="Coelomata" taxId="33316" hidden="true"/>            <taxon scientificName="Bilateria" taxId="33213" hidden="true"/>            <taxon scientificName="Eumetazoa" taxId="6072" hidden="true"/>            <taxon scientificName="Metazoa" commonName="metazoans" taxId="33208" rank="kingdom"/>            <taxon scientificName="Fungi/Metazoa group" taxId="33154" hidden="true"/>            <taxon scientificName="Eukaryota" commonName="eucaryotes" taxId="2759" rank="superkingdom"/>            <taxon scientificName="cellular organisms" taxId="131567" hidden="true"/>            <taxon scientificName="root" taxId="1" hidden="true"/>        </lineage>

I want the scientificName only when it is not hidden (hidden="true") is not present and in reverse order, starting from bottom like:Eukaryota;Metazoa;Chordata;Craniata;Vertebrata;Euteleostomi;Mammalia;Eutheria;Euarchontoglires;Primates;Haplorrhini;Catarrhini;Hominidae;HomoThis is a new thing for me. Can you kindly help me.Thank you,Sammed

Link to comment
Share on other sites

Hey Experts, Though I got the solution for the above problem, I am unable to deal with a minute problem. The code for the above problem is

<field name="lineage"> <xsl:for-each select="reverse(lineage/taxon)">               <xsl:choose>                    <xsl:when test="@hidden !=' '"/>                    <xsl:otherwise>			                      <xsl:for-each select="./@scientificName">                             <xsl:value-of select="."/>; </xsl:for-each>                      </xsl:otherwise>                </xsl:choose>                </xsl:for-each> </field>

and the output it gives as follows

<field name="lineage">Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi; Mammalia; Eutheria; Euarchontoglires; Primates; Haplorrhini; Catarrhini; Hominidae; Homo; </field>

except for the last ';' after Homo. I tried xsl:if statement for position() != last(), but I think I am going wrong somewhere; your valuable suggestions would definitely help me.Thank you.Sammed

Link to comment
Share on other sites

It is simpler than you tried:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">    <xsl:template match="lineage">	<field name="lineage">	  <xsl:value-of select="reverse(taxon[not(@hidden = 'true')]/@scientificName)" separator=";"/>	</field>  </xsl:template>  </xsl:stylesheet>

That is XSLT 2.0 (which you seem to use as you did use "reverse"). XSLT 2.0 can be run with processors like Saxon 9 or AltovaXML or XQSharp.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...