[xsl] testing for existence of content before processing

Subject: [xsl] testing for existence of content before processing
From: Terry Ofner <tdofner@xxxxxxxxx>
Date: Mon, 18 Oct 2010 13:12:47 -0400
I am working with a table of contents that I am converting into a table,
placing the contents of the toc into one column of the table and inserting
certain content in the last column of each row, depending upon the position
and content of certain entries. All is great until I get to situations in
which the content depends upon the existence or non-existence of a certain
item in the toc.

Given this incoming xml...

<chapter name="Chapter 1">
	<tocItem><title>Lesson A</title><pages>pp. 4-5</pages></tocItem>
	<tocItem><title>Lesson B</title><pages>pp. 6-7</pages></tocItem>
	<tocItem><title>Lesson C</title><pages>pp. 8-9</pages></tocItem>
	<tocItem><title>Lesson D</title><pages>pp. 10-11</pages></tocItem>
	<tocItem><title>Lab</title><pages>p. 12</pages></tocItem>
	<tocItem><title>Test</title><pages>pp. 13-14</pages></tocItem>
</chapter>

I would put a review item in the same row as <title>Lab</title>.

Given this chapter...

<chapter name="Chapter 2">
	<tocItem><title>Lesson A</title><pages>pp. 15-16</pages></tocItem>
	<tocItem><title>Lesson B</title><pages>pp. 16-17</pages></tocItem>
	<tocItem><title>Lesson C</title><pages>pp. 18-19</pages></tocItem>
	<tocItem><title>Lesson D</title><pages>pp. 20-21</pages></tocItem>
	<tocItem><title>Test</title><pages>p. 22</pages></tocItem>
</chapter>

I would put the review item in the same row as Lesson D.

My question, how do I test for the existence or non-existence of the Lab
before I start processing the chapter? If the review always fell in the
second-to-last row, I would have no problem. But sometimes the Lab falls two
or three rows before the last row.

Currently, I am using a combination of position() tests and predicates to
populate the rows. For example:

<xsl:for-each select="tocItem">
	<xsl:choose>
		<xsl:when test="position()=1">
			<!--place pretest-->
		</xsl:when>
		<xsl:when test="position()=last()">
			<!--place posttest-->
		</xsl:when>
		<xsl:when test="self::tocItem[title = 'Lab']">
			<!--place review-->
		</xsl:when>
		<xsl:otherwise>
			<!--place generic row info-->
		</xsl:otherwise>
	</xsl:choose>

As you can see, a chapter with no Lab will receive no review.

Perhaps my approach is wrong-headed. Any assistance would be most
appreciated.

Terry

Current Thread