This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: returning single result from apply-templates



   > 
   > > If I do
   > > 
   > >  <xsl:apply-template select="foo//bar[1]"/>
   > > 
   > > I get mutltiple results ...
   > > because each <bar> is #1 in its sequence, whereas I really only want
   > > the first <bar> from the result sequence.
   > 
   > Do select="(foo//bar)[1]"

   Which is not the same as select="foo//bar[1]"
   but is the same as select="foo/descendant::bar[1]"/>


No it's not the same as foo/descendant::bar[1] (or the other suggested
answer of foo/x[1]/bar)


foo//bar[1]
  All the bar which are the first child of any descendent of any foo
  child of the current node. 

foo/descendant::bar[1]
  All the bar which are the first descendant of any foo child of the
  current node.

foo//bar
  All the bar which are descendants of any foo child of the current
  node.

(foo//bar)[1]
  The first node in document order in the node set constructed above.
  Note if there is more than one foo child this is the only one
  guaranteed to select at most one node



   Whats the plain English version of this please.

Was that plain enough?

   Just what do the () do here?

Make the [] apply to the whole node set constructed by foo//bar rather
than just the last step in //bar[1].

   <quote>NOTE: The meaning of a Predicate depends crucially on which axis
   applies. For example, preceding::foo[1] returns the first foo element in
   reverse document order, because the axis that applies to the [1] predicate
   is the preceding axis; by contrast, (preceding::foo)[1] returns the first
   foo element in document order, because the axis that applies to the [1]
   predicate is the child axis.</quote>


Ah. This quote is just the spec being deliberately obscure. The child
axis stuff is just a red herring.

What it means is that the meaning of [1] depends on the axis
child::*[1] picks the first child, ancestor::*[1] picks the parent (not
the first ancestor in document order)
but some expressions, if they are not just a single step don't have an
axis. eg (foo//bar)[1] or (following::x | ancestor::y)[2] in these cases
document order is always used. But rather than say document order is
used the spec says the child axis applies. But the only use of the axis
here is to determine that position() refers to document rather than
reverse document order.

David


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]