[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] the nearest ancestor with the attribute


Subject: Re: [xsl] the nearest ancestor with the attribute
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 15 Jan 2001 17:19:08 +0000

Hello Guai,

ancestor::*[@attr]
  gets you the ancestors with the 'attr' attribute, from nearest to furthest

ancestor::*[@attr][1]
  gets you the first of these

ancestor::*/@attr
  gets you the attribute nodes themselves (from nearest to furthest)
  this is often good enough, because if (for example), you ask for the
  value of this node set, you'll get the value of the first one (the nearest)

ancestor::*/@attr[normalize-space(.)]
  gets you the same set, only excluding all attributes from the list
  that have no value or a whitespace-only value

the trick is, however, that
ancestor::*/@attr[1]
  won't get the nearest attr attribute, but the whole set
  this is because [position()=1] will test true for any attribute
  (won't it?)

if you want to filter the nodes so only the attribute on the nearest ancestor
with the attribute is returned, you have to do

(ancestor::*/@attr)[last()]

This takes the node set of all the attr attributes on all ancestors, groups them in document order, and takes the last in the list. This will be the nearest.

Hope that helps!
Wendell




At 01:45 PM 1/15/01 -0800, you wrote:
How could I get the nearest ancestor with a certain attribute, say "attr"?

Thanks,
Guai

====================================================================== 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 ======================================================================


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




Current Thread
Keywords
xml