Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 September 22nd, 2006, 06:39 AM
Authorized User
 
Join Date: May 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting a heading only in only the first element

I would like to insert a heading for the first child element in another element, and not for any others that may follow. For example, if I have XML like this:
--------------------------------------

<article>
    <section>
        <information>
        some stuff
        </information>

        <question>
        one
        </question>

        <question>
           two
        </question>

    </section>

    <section>
        <information>
        some stuff
        </information>

        <question>
        three
        </question>

        <question>
            four
        </question>

    </section>

</article>
-----------------------------------

And my XSLT looks something like this:

------------------------------------

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

        </head>
        <body>
        <xsl:apply-templates/>

        </body>
    </html>
</xsl:template>

<xsl:template match="information">
<b><xsl:apply-templates/></b>
</xsl:template>

<xsl:template match="question">
<h3>Test My Knowledge</h3>
<h4>Question <xsl:number level="any"/></h4>

</xsl:template>

</xsl:stylesheet>
------------------------------------

Then I get the following result:

------------------------------------

some stuff
Test My Knowledge
Question 1
Test My Knowledge
Question 2
some stuff
Test My Knowledge
Question 3
Test My Knowledge
Question 4
--------------------------------------

But I would like the "Test my Knowledge" to apeear only in front of the first "question" element in every "section" element, something like this:

----------------------------------------

some stuff
Test My Knowledge
Question 1
Question 2

some stuff
Test My Knowledge
Question 3
Question 4

---------------------------------------

Is it possible to do this without changing the structure of the XML?








 
Old September 22nd, 2006, 07:04 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Since you want the heading output once for each information element, just change your code to produce it as part of the template that processes information elements.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 28th, 2006, 08:06 AM
Authorized User
 
Join Date: Sep 2006
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Stekker,

I used this syntax to pick out the first entry ...

<xsl:apply-templates select="/dataroot/xmlout[1]/REPTITLE" />

REPTITLE is a field containing the title of the report and [1] tells the processor to take just the first occurance (I believe).

Hope this helps you :-)

Cheers,
Alan.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting/Creating element in order whelanm XSLT 3 August 14th, 2008 12:54 PM
display no heading if null oleafo ASP.NET 1.0 and 1.1 Basics 1 January 8th, 2007 06:23 PM
inserting element wihout template match gonzalocordero XSLT 2 July 3rd, 2006 12:46 PM
Cannot Modigy Heading wongcalvin Beginning PHP 1 December 12th, 2004 11:05 PM
Inserting one element into another srini XSLT 3 January 21st, 2004 08:09 AM





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