Re: [xsl] testing for existence of content before processing

Subject: Re: [xsl] testing for existence of content before processing
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 18 Oct 2010 17:28:26 -0400
Terry,

I don't think your effort is so bad. It seems your rules are something like:

Always want a pretest with the first item
Always want a posttest with the last item
Always want a review with a "Lab" item
If no "Lab" item is present, place a review with the penultimate item

So:

<xsl:when test="position()=1">
  <!--place pretest-->
</xsl:when>
<xsl:when test="position()=last()">
  <!--place posttest-->
</xsl:when>
<xsl:when test="title = 'Lab' or
                ((position()=last()-1) and not(../*/title='Lab')">
  <!--place review-->
</xsl:when>
<xsl:otherwise>
  <!--place generic row info-->
</xsl:otherwise>

I can imagine refactoring this, and I have a concern about edge cases. (What if the Lab appears last?) I also suspect that the generic row info appears in the other branches as well, which suggests it could be pulled out. So it might look something like:

<!--place generic row info-->
<xsl:if test="not(preceding-sibling::tocItem)">
  <!--place pretest-->
</xsl:if>
<xsl:if test="title = 'Lab' or (count(following-sibling::tocItem)=1 and
                                not(../tocItem/title='Lab'))">
  <!--place review-->
</xsl:if>
<xsl:if test="not(following-sibling::tocItem)">
  <!--place posttest-->
</xsl:if>

This time I've used paths instead of position tests to establish the location. And if more than one condition obtains, you get whatever it entails, in an order you want.

Cheers,
Wendell

At 01:12 PM 10/18/2010, you wrote:
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.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread