Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 19th, 2005, 04:31 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>
Reply With Quote
  #2 (permalink)  
Old September 19th, 2005, 05:06 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old September 19th, 2005, 05:17 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
  #4 (permalink)  
Old September 19th, 2005, 05:22 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.


Reply With Quote
  #5 (permalink)  
Old September 19th, 2005, 05:44 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
translate element name to element name lexzeus XSLT 3 September 4th, 2006 09:04 AM
Element Neal XSLT 1 June 13th, 2006 03:57 AM
Finding the last element jeremycod XSLT 4 March 12th, 2006 05:45 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM



All times are GMT -4. The time now is 02:11 PM.


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