Subject: Inserting a heading only in only the first element
Posted By: stekker Post Date: 9/22/2006 6:39:20 AM
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?








Reply By: mhkay Reply Date: 9/22/2006 7:04:06 AM
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
Reply By: asearle Reply Date: 9/28/2006 8:06:54 AM
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.


Go to topic 50357

Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159
Return to index page 158
Return to index page 157
Return to index page 156
Return to index page 155
Return to index page 154
Return to index page 153