|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

September 22nd, 2006, 07:39 AM
|
|
Authorized User
|
|
Join Date: May 2006
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

September 22nd, 2006, 08:04 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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
|

September 28th, 2006, 09:06 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |