p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Can I tell what element I am currently in? (http://p2p.wrox.com/showthread.php?t=33497)

insanek September 19th, 2005 04:31 AM

Can I tell what element I am currently in?
 
Hi,
I have an XML document made up of <P></P> and <L></L> (as well as other things).
My XSLT renders (as you might of guessed) the <P>'s as HTML paragraphs and <L>'s as lists.
Currently it renders all of the paragraphs first and then the lists.
I think, I need a way to figure out what element (don't know if that's the right word) I am in, then apply the template as needed.
i.e.
Code:

if (current_element == P)
{
<p>current_element.contents</p>
}
else if (current_element == L)
{[list]
<li>current_element.contents</li>
</ul>
}

I have tried using current() and every combination of * . // etc etc I can think of, but to no avail.

Any help, would be greatly appreciated!
-thanks
Alex.

this is the XSLT I currently have (below), I tried to post my XML as well, but its too big and timed out :( ...
Code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xmlt1-transitional.dtd" doctype-public="-//W3C/DTD XHTML 1.0 Transitional//EN" indent="yes"/>



    <xsl:template match="/">
        <html>
            <head>

                <title>Asthma XSL test</title>
                <link href="../styles/global.css" media="screen" type="text/css" rel="stylesheet"/>
            </head>
            <body>
                <a name="top"/>
                <h1>
                    <img height="45" alt="AstraZenenca" src="../images/azlogo.gif" width="175"/>
                </h1>
                <div id="container">

                    <h2>

                        <xsl:value-of select="/TaggedPDF-doc/P[1]"/>
                        <br/>

                        <xsl:value-of select="/TaggedPDF-doc/H1[2]/text()[1]"/>
                    </h2>



                    <div id="accessibilityNotice">
                        <p>AstraZeneca has delivered this text only version of the course to serve those users who have difficulty accessing the interactive version.</p>
                        <p>You can access it here:
                            <a href="#">Full Interactive Version</a>
                        </p>
                    </div>



                    <ol>
                        <xsl:for-each select="//H2 | //P[translate(substring(.,1,1), '0123456789', '9999999999') = '9' and translate(substring(.,3,1), '0123456789', '9999999999') = '.'] ">

                            <li value="0">
                                <a href="#{generate-id()}">
                                    <xsl:value-of select="."/>
                                </a>
                            </li>
                        </xsl:for-each>
                    </ol>


                    <p>
                        <strong>
                            <xsl:value-of select="substring(TaggedPDF-doc/Part/P,1,17)"/>
                        </strong>
                        <xsl:value-of select="substring(TaggedPDF-doc/Part/P,18,100)"/>
                    </p>

                    <xsl:apply-templates select="TaggedPDF-doc/Part/Sect"/>
                </div>
            </body>
        </html>
    </xsl:template>


    <xsl:template match="Sect">
        <xsl:apply-templates select="H2"/>

    <!--    <xsl:for-each select="//P | //L">

            <xsl:choose>
                <xsl:when test="*[current()=P]"> -->
                    <xsl:apply-templates select="P"/>
            <!--    </xsl:when>


                <xsl:when test="*[current()=L]"> -->
                    <xsl:apply-templates select="L"/>
    <!--            </xsl:when>
            </xsl:choose>
        </xsl:for-each>
 -->





        <xsl:if test="*[position()=last()]">
            <p class="backtotop">
                <a href="#top">Back to Top</a>
            </p>
        </xsl:if>
    </xsl:template>










    <xsl:template match="H2">
        <a name="{generate-id()}"></a>
        <h3>
            <xsl:value-of select="."/>
        </h3>
    </xsl:template>



    <xsl:template match="P">
        <xsl:choose>


            <xsl:when test="text()[starts-with(.,'Introduction')]">
                <h4>
                    <xsl:value-of select="."/>
                </h4>
            </xsl:when>


            <xsl:when test="text()[starts-with(.,'Transcript')]">
                <h4>
                    <xsl:value-of select="."/>
                </h4>
            </xsl:when>


            <xsl:when test="text()[starts-with(.,'Table Text')]">
                <p>
                    <strong>
                        <xsl:value-of select="substring(.,1,10)"/>
                    </strong>
                    <xsl:value-of select="substring(.,11,500000000)"/>
                </p>
            </xsl:when>



            <!--Caters for titles that are wrongly in P tags in the XML, by seeing if they start with a 2 digit number and a "." 
    and creates an anchort for them-->
            <xsl:when test="translate(substring(.,1,1), '0123456789', '9999999999') = '9' and  translate(substring(.,3,1), '0123456789', '9999999999') = '.'">
                <a name="{generate-id()}"></a>
                <h3>
                    <xsl:value-of select="."/>
                </h3>
            </xsl:when>


            <xsl:when test="text()[starts-with(.,'Rollover Text')]">
                <p>
                    <strong>
                        <xsl:value-of select="substring(.,1,14)"/>
                    </strong>
                    <xsl:value-of select="substring(.,15,500000000)"/>
                </p>
            </xsl:when>


            <!--
        <xsl:when test="//* = preceding-sibling::*[text()[starts-with(.,'Rollover Text:')]]">
            <strong><xsl:value-of select="."/></strong>
        </xsl:when>-->



            <xsl:otherwise>
                <p>
                    <xsl:value-of select="."/>
                </p>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>



    <xsl:template match="L">
        [list]
            <xsl:for-each select="LI">
                <li>
                    <xsl:value-of select="LI_Title"/>
                </li>
            </xsl:for-each>
        </ul>
    </xsl:template>
</xsl:stylesheet>


mhkay September 19th, 2005 05:06 AM

This is what template rules are for.

In your template rule for match="sect"

<xsl:template match="Sect">
        <xsl:apply-templates select="H2"/>

       <xsl:for-each select="//P | //L">

The "//" is clearly nonsense. This processes all the P and L elements (anywhere in the document) while you are processing each sect. You only want to process the children.

Just do apply-templates select="P|L"

and then write one template rule for match="P" and another for match="L".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

insanek September 19th, 2005 05:17 AM

The
Code:

<xsl:for-each select="//P | //L">
was just me trying every thing I could possibly think off (ha ha).
But i'll give that a go, thanks for the help.


insanek September 19th, 2005 05:22 AM

That worked a treat!
I didn't realise you could have a single apply-templates select="",
then have two templates comming off it.

I learn something new every day, thanks for the help.



mhkay September 19th, 2005 05:44 AM

>The
Code:

<xsl:for-each select="//P | //L">
was just me trying every thing I could possibly think of.

Really, trial and error is a very poor method of coding. Sometimes you hit on things that seem to work, but only work because you haven't written enough test cases. Try to avoid it: you need to understand the semantics of the language you are using.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


All times are GMT -4. The time now is 06:29 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.