Re: [xsl] Pattern question: first child of first descendant

Subject: Re: [xsl] Pattern question: first child of first descendant
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 24 Mar 2009 15:37:24 -0400

At 12:45 PM 3/24/2009, you wrote:
>>match="entry[. is
>This will match the first title or para child of a first entry
>descendant of a table, but if you have
>   <label/>
>   <title/>
>   <para/>
>the title will be matched.
>I think the spec needs a bit of clarification. :-)

Sorry for being imprecise, the intent is to match the first title or
para in a cell, whichever comes first. It doesn't matter whether there
are other elements before it (label in your example).

Not a problem. Given what you say, I believe Mike read this one correctly.

I'm not answering to the many helpful posts on this individually.
Andrew's link on "//" was very interesting (the gotcha would have resp.
has got me for sure). I'll try to summarize the idea behind the solution:

1. Patterns are limited. If you cannot do it directly, shove the logic
into predicates.

That words, and there are also other techniques for getting around the limitations in patterns.

(Nor am I one to complain about those limitations.)

2. In the predicate, use the ancestor axis to "go back" to the element
you wanted to start from originally, then formulate your path as you
would have done without patterns' limitations and check for node
identity on the target.

Hope I got that sufficiently right, though it reads not much clearer :-
( Another try:

Instead of



match="target[. is pathspec_to/anchor/unsupported_pathspec_to/target]"

That works. So, often, does not using a pattern at all. For example, let's say you had a bit of run-in text that you wanted to lead only the first title or para inside the first entry inside a table:

<xsl:template match="entry/title | entry/para">
  <h5 class="entry-title">
    <xsl:call-template name="run-in"/>

<xsl:template match="entry/para">
    <xsl:call-template name="run-in"/>

<xsl:template name="run-in">
  <!-- a run-in label is inserted into the first title or para that
       appears in the first entry of a table -->
  <xsl:if test=". is ancestor::table[1]/descendant::entry[1]/(title|para)[1]">

This may not compile into anything quite so tight as doing it with a match pattern, but it is arguably easier to read and maintain, especially by the XSLT neophyte who takes over your stylesheet. And the logic is very flexible and adaptable.


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
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