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 February 2nd, 2012, 09:27 AM
seb seb is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditionally selecting following element

I have a snippet of xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<sect>
	<Link>3</Link>
	<Link>4</Link>
	<Link>5</Link>
	<footnote>3 this is the footnote text</footnote>
	<footnote>4 this is the footnote text</footnote>
	<footnote>5 this is the footnote text</footnote>
</sect>
I want to transform it with xslt 1.0 to:

Code:
<span class="link" title="3 this is the footnote text">[3]</span>
<span class="link" title="4 this is the footnote text">[4]</span>
<span class="link" title="5 this is the footnote text">[5]</span>
What I have tried so far is:

Code:
<xsl:template match="Link">
    <span class="link">	
		    <xsl:attribute name="title"><xsl:value-of select="following::footnote[1]"/></xsl:attribute>
		    [<xsl:value-of select="."/>]
    </span>	
</xsl:template>
This returns
Code:
<span class="link" title="3 this is the footnote text">[3]</span>
<span class="link" title="3 this is the footnote text">[4]</span>
<span class="link" title="3 this is the footnote text">[5]</span>
I am not sure how to do a test on the following footnote in order to get past the first one and check the following ones.
 
Old February 2nd, 2012, 10:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the task is really as you describe it, then the output depends only on the footnote elements and not on the Link elements. So you could just ignore the Link elements and process the footnotes.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 2nd, 2012, 10:10 AM
seb seb is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That isn't the case... The links appear within paragraph text and I want the spans to appear in those appropriate positions.

Such as:

This is advice about swimming [1] off the Dorset Coast. Next sentence [4] etc...
 
Old February 2nd, 2012, 10:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

OK, so the input isn't what you showed us, and we're expected to guess what it's really like.

Isn't it possible to use some structured markup to identify the footnotes, for example

Code:
<footnote nr="3">This is the third footnote</footnote>
That would make things much easier, especially if you are using XSLT 1.0.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 2nd, 2012, 11:10 AM
seb seb is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Apologies, I hadn't appreciated that the xml would be interpreted that way:

<code>
<sect>
<P>It is widely accepted that the world’s climate asdfsadfsdf sdf asdf sdf afd a </P>
<P>Over the past century, the Earth has asdf sdfsadf asdf sdfadsf
<Link>3</Link>
. adsf safd sadf sadfsadf sadf sadf sad fsad
<Link>4</Link>
.asd asd sadf sadfsadf sadf sadf sadf sadf asdf sadfsad d </P>
<P>blah blah blah </P>
<footnote>3 Global temperature trends 1911-2010 asdf sadfsadf sadfsad fasdf</footnote>
<footnote>4 Global temperature trends 1975-2010 were: Hasdf sad fsafd sa </footnote>
</sect>
</code>


I would like the links to appear where they are in the paragraphs. I won't be displaying the footnotes at the foot of the html displayed page but will have a hover over the links that will display the footnotes.
 
Old February 2nd, 2012, 11:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Define a key

Code:
<xsl:key name="n" match="footnote" use="substring-before(., ' ')"/>
and use it like this:

Code:
<xsl:template match="Link">
...
<xsl:value-of select="key('n', .)"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 2nd, 2012, 12:08 PM
seb seb is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael, that's perfect.





Similar Threads
Thread Thread Starter Forum Replies Last Post
conditionally setting variable ewel Beginning PHP 2 December 13th, 2007 07:39 PM
conditionally selecting buttons R4addZ Visual Basic 2005 Basics 3 July 13th, 2007 04:26 PM
Conditionally display text mister_mister XSLT 6 March 9th, 2007 11:02 AM
Conditionally display text object leo_vinay Crystal Reports 1 April 18th, 2006 07:22 AM





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