[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: testing for existence of content before processing

Subject: Re: testing for existence of content before processing
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 18 Oct 2010 17:28:26 -0400
Re:  testing for existence of content before processing
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.