[XML4Lib] difficulty with following-sibling axis in XSLT

Conal Tuohy Conal.Tuohy at vuw.ac.nz
Mon May 26 19:09:59 EDT 2008


Hi John

I such a circumstance I really recommend the use of a key. 

<!-- index every "info" span by its first preceding "title" span -->
<xsl:key 
   name="info-by-title" 
   match="span[@class='info']" 
   use="preceding-sibling::span[@class='title'][1]"/>

Then you can use the XSLT "Key" function to retrieve the info spans for a title. Using your example:

<xsl:template match="library/span[@class='title' and .='Wuthering 
Heights']">
	<xsl:for-each select="key('info-by-title', .)">
		<p>
			<xsl:value-of select="."/>
		</p>
	</xsl:for-each>
</xsl:template>

Of course you can achieve the same effect with complex xpath expressions, but using a key is usually much simpler and clearer.

Conal

-----Original Message-----
From: xml4lib-bounces at webjunction.org on behalf of jfitzgibbon at galwaylibrary.ie
Sent: Tue 5/27/2008 3:58
To: xml4lib at webjunction.org
Subject: [XML4Lib] difficulty with following-sibling axis in XSLT
 
Hi,

I have an XML file which I saved from a HTML file and it has the following 
structure.

<library>
	<span class="title">Wuthering Heights</span>
	<span class="info">paperback</span>
	<span class="info">unavailable</span>
	<span class="title">Tom Sawyer</span>
	<span class="info">Audio Book</span>
	<span class="info">Available</span>
	<span class="info">Unabridged</span>
</library>

The problem is that all span tags with an attribute of info are siblings 
of each other and I have no way of knowing how many span tags with a class 
of info occur after each title. For example, if I try to access all the 
information about 'Wuthering Heights' and only 'Wuthering Heights' I am 
running into problems.

For example, if I try

<xsl:template match="library/span[@class='title' and .='Wuthering 
Heights']">
	<xsl:for-each select="following-sibling::span[@class='info']">
		<p>
			<xsl:value-of select="."/>
		</p>
	</xsl:for-each>
</xsl:template>

I will retrieve all information on all books and not just the information 
on "Wuthering Heights".

I get

<p>paperback</p>
<p>unavailable</p>
<p>Audio Book</p>
<p>Available</p>
<p>Unabridged</p>

How do I limit my results to those span elements containing information on 
"Wuthering Heights"?

This is a problem which I encounter frequently when I convert a HTML 
document to XML.

I would appreciate any advice.

Regards
John



_______________________________________________
XML4Lib mailing list
XML4Lib at webjunction.org
http://lists.webjunction.org/mailman/listinfo/xml4lib






More information about the XML4Lib mailing list