Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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!
 
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
 
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>
 
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!
 
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





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





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