Re: [xsl] xsl conditional: finding the right node

Subject: Re: [xsl] xsl conditional: finding the right node
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 12 Feb 2004 22:08:47 -0500
At 2004-02-12 21:21 -0500, Allin Cottrell wrote:
I'm processing a long list of elements (a command reference for a
computer program).  The enclosing element is "commandlist" and the
individual element is "command".

Fine.


My problem is this: when I process each "command", I need to check
whether or not it has any sub-elements (arguments, options) of a sort
that would require a display table to be built.

XPath tests can return true/false on the presence of child or descendent nodes.


One could explicitly write this down ("needs-table=true|false") as an
attribute of each "command", but I would rather automate it.  I'm
trying to figure out how XSL can know the answer before it reaches the
elements that would need a prior table-start -- and, of course, re-set
the answer when the next "command" comes along.

That is thinking too much like a serial programmer, and not a hierarchical processor. Think branches and leaves, or better still, parents, children, siblings, ancestors and descendants.


Try not to think of variables (in XSLT variables do not vary) ... check out the inherent facilities in XSLT for processing hierarchies.

The pseudo-code for what I'm trying to implement is something like:

Find a "command" element
  Read ahead to see if a table is wanted (does this node have any
                     children of a certain sort?)
    Output basic command info, before table
    Output table-start if wanted
      Output table rows if applicable ...
    Output table-end if wanted
    Continue processing rest of command node

Next command...

<xsl:for-each select="command"> <xsl:for-each select="basic-command-element-child-stuff"> </xsl:for-each> <xsl:if test="table-stuff-element-child"> <table> <xsl:for-each select="table-stuff-element-child"> ... </xsl:for-each> </table> </xsl:if> <xsl:for-each select="other-stuff"> </xsl:for-each> </xsl:for-each>

The above takes a "pull approach" to the information ... it is easy but less flexible for specialization ... a "push approach" would use multiple template rules and <xsl:apply-templates/>. It's not hard, but I find my students embrace the pull approach much more quickly and I have to push them to use the push style.

I hope this helps.

..................... Ken

--
Public courses: upcoming world tour of hands-on XSL training events
Each week:    Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO
Washington, DC: 2004-03-15            San Francisco, CA: 2004-03-22
Hong Kong: 2004-05-17    Germany: 2004-05-24    England: 2004-06-07
World-wide on-site corporate, government & user group XML training!

G. Ken Holman                  mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.           http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0     +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness   http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread