2

I have tried to perform nested loop in the XSL file but got stuck after several attempt

My xml file goes like this way

<chromosome cnumber="X" cstart="10000" cend="1000000">
    <gene>
        <gname>ENSG00000216667</gname>
        <gstart>100411</gstart>
        <gend>102713</gend>
        <external_ref>
            <one>OTTG:OTTHUMG00000046372</one>
            <two>HGNC:CXYorf11</two>
            <three>HGNC_curated_gene:CXYorf11</three>
        </external_ref>
        <transcript>
            <tname>ENST00000406851</tname>
            <tstart>100411</tstart>
            <tend>102713</tend>
            <tstrand>+1</tstrand>
        </transcript>
    </gene>
    <gene>
        <gname>ENSG00000182378</gname>
        <gstart>122990</gstart>
        <gend>150024</gend>
        <external_ref>
            <one>UCSC:uc004cpa.1</one>
            <two>UCSC:uc004cpb.1</two>
            <three>HGNC:PLCXD1</three>
            <four>HGNC_automatic_gene:PLCXD1</four>
        </external_ref>
        <transcript>
            <tname>ENST00000381657</tname>
            <tstart>127860</tstart>
            <tend>150024</tend>
            <tstrand>+1</tstrand>
        </transcript>
        <transcript>
            <tname>ENST00000399012</tname>
            <tstart>122990</tstart>
            <tend>150021</tend>
            <tstrand>+1</tstrand>
        </transcript>
        <transcript>
            <tname>ENST00000381663</tname>
            <tstart>122992</tstart>
            <tend>150021</tend>
            <tstrand>+1</tstrand>
        </transcript>
    </gene>
    <gene>
        <gname>ENSG00000214798</gname>
        <gstart>148481</gstart>
        <gend>149027</gend>
        <external_ref>
            <one>UCSC:uc004cpc.1</one>
            <two>Clone_based_ensembl_gene:BX000483.7</two>
        </external_ref>
        <transcript>
            <tname>ENST00000399005</tname>
            <tstart>148481</tstart>
            <tend>149027</tend>
            <tstrand>+1</tstrand>
        </transcript>
    </gene>
    <gene>
        <gname>ENSG00000178605</gname>
        <gstart>150026</gstart>
        <gend>160887 </gend>
        <external_ref>
            <one>UCSC:uc004cpe.1</one>
            <two>HGNC:GTPBP6</two>
            <three>HGNC_automatic_gene:GTPBP6</three>
        </external_ref>
        <transcript>
            <tname>ENST00000326153</tname>
            <tstart>150026</tstart>
            <tend>160887</tend>
            <tstrand>-1</tstrand>
        </transcript>
    </gene>
    <gene>

Here I tried XSLT this way

<xsl:for-each select="chromosome/gene">
    Name: <xsl:value-of select="gname" /> <br />
    Start Region: <xsl:value-of select="gstart" /> <br />
    End Region: <xsl:value-of select="gend" /> <br /> 
    <xsl:value-of select="external_ref/one "/><br />
    <xsl:value-of select="external_ref/two "/>
    <xsl:if test="external_ref/three !=' '"><br/>
        <xsl:value-of select="external_ref/three "/>
    </xsl:if>
    <xsl:if test="external_ref/four !=' '"><br/>
        <xsl:value-of select="external_ref/four "/>
    </xsl:if>
    <xsl:for-each select="chromosome/gene/transcript">
        Name:<xsl:value-of select="tname" /></strong> <br />
        Start Region: <xsl:value-of select="tstart" /> <br />
        End Region: <xsl:value-of select="tend" /> <br />
        Strand: <xsl:value-of select="tstrand" />
    </xsl:for-each>
</xsl:for-each>

The problem is I am not getting any transcript; when I use only one for each loop at the top then I get only one transcript not whole from the xml list

Can you guys point my mistake, I am completely new to XSLT.

Thanks

7
  • Please note that your source XML is not well-formed. Commented Apr 8, 2011 at 15:20
  • And now that I've looked closer, neither is your XSLT Commented Apr 8, 2011 at 15:21
  • Well formed in a sense that there are no indentation? Commented Apr 8, 2011 at 15:29
  • @thchand - ...in the sense that there is a closing </strong> with no matching start tag in the XSLT and a couple of elements in the source XML that aren't closed. Commented Apr 8, 2011 at 15:34
  • 1
    @thchand: Also do note that this nested xsl:for-each instructions are not the proper XSLT style. You need to learn about pattern matching. Commented Apr 8, 2011 at 15:40

1 Answer 1

7

With the outer <xsl:for-each loop you already stand at the level of chromosome/gene. Think of it as in "changing into that directory" in a file system.

When you want the transcript from within there, you need to only select this like:

<xsl:for-each select="transcript">
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, now I got it. But, couldn't vote due to reputation
@thchand - you can accept though - even with low reputation - by clicking the checkmark. If I recall correctly you will even get a badge for it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.