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 July 24th, 2003, 10:03 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT - First Time Matching a Template

I am writing a XSLT stylesheet to convert XML to HTML (as many people do).

Well my dilema is that I need to output a certain row of a table ONLY the first time the template match occurs.

For example-

xml:
<parent><child>jack</child><child>jill</child><child>joe</child></parent>

html:
<table>
<tr><td>IVE GOT CHILDREN!</td></tr>
<tr><td>jack</td></tr>
<tr><td>jill</td></tr>
<tr><td>joe</td></tr>
</table>


this is what im trying to accomplish, where the "IVE GOT CHILDREN!" displays the first time on the match of child node, however the other two times it does not output.

This is difficult without true "variables" in XSLT.

Please help!
Reply With Quote
  #2 (permalink)  
Old July 25th, 2003, 12:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

You don't need variables here, just use position() function.

Assuming that the source XML is
Code:
<parent><child>jack</child><child>jill</child><child>joe</child></parent>
two main templates are:
Code:
    <xsl:template match="parent">
        <table>
            <xsl:apply-templates select="*"/>
        </table>
    </xsl:template>

    <xsl:template match="child">
        <xsl:if test="position() = 1">
            <tr>
                <td>IVE GOT CHILDREN!</td>
            </tr>
        </xsl:if>
        <tr>
            <td><xsl:value-of select="."/></td>
        </tr>
    </xsl:template>
Although I would prefer to put the header in the "parenet" template body:
Code:
    <xsl:template match="parent">
        <table>
            <tr>
                <td>IVE GOT CHILDREN!</td>
            </tr>
            <xsl:apply-templates select="*"/>
        </table>
    </xsl:template>

    <xsl:template match="child">
        <tr>
            <td><xsl:value-of select="."/></td>
        </tr>
    </xsl:template>
Regards,
Armen
Reply With Quote
  #3 (permalink)  
Old July 25th, 2003, 04:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes but if you're going to include the header in the parent, you should also include a test to make sure there are children before printing the row:
Code:
    <xsl:template match="parent">
        <table>
            <xsl:if test="count(child) &gt; 0">
                <tr>
                    <td>IVE GOT CHILDREN!</td>
                </tr>
            </xsl:if>
            <xsl:apply-templates select="*"/>
        </table>
    </xsl:template>
Reply With Quote
  #4 (permalink)  
Old July 25th, 2003, 06:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

But parents always have child(ren) by definition :)

Yes of course, you are right!
Reply With Quote
  #5 (permalink)  
Old July 25th, 2003, 07:50 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

very good

placing it in the parent element worked nicely (with the check for children, which I do need in some cases), but I also learned how to use the position() function which should come in handy later

thank you gentlemen

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
Stumped on XSLT template hayedid XSLT 8 December 19th, 2007 02:14 PM
XSLT: ONE template to transform all the elements Behl_Neha XSLT 8 December 15th, 2007 07:31 PM
THB old Template... it´s time for a new look kherrerab BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 October 5th, 2007 05:42 PM
XSLT Template Issue saravanan.k XSLT 2 March 14th, 2007 06:41 AM
XSLT Template error again bmains XSLT 4 December 19th, 2003 11:17 AM



All times are GMT -4. The time now is 04:57 AM.


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