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;Homo..
This is a new thing for me...Can you kindly help me..

Thank you,
Sammed

Recommended Answers

All 3 Replies

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

result with xsl1

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
	<xsl:template match="/">
		<xsl:apply-templates select="lineage"/>
	</xsl:template>
	<xsl:template match="lineage">
		<field name="{local-name()}">
			<xsl:apply-templates select="taxon[not(@hidden)]">
				<xsl:sort data-type="number" select="position()" order="descending"/>
                                <!-- Here is the order reversed -->
			</xsl:apply-templates>
		</field>
	</xsl:template>
	<xsl:template match="taxon">
		<xsl:choose>
			<xsl:when test="position()=last()">
				<xsl:value-of select="@scientificName"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@scientificName"/>
				<xsl:text>;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>

Hey, this helps. Thank you for your help.

Thank you,
Sammed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.