Re: [xsl] select elements based on another element's attributes

Subject: Re: [xsl] select elements based on another element's attributes
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 24 Jul 2002 22:24:46 +0100
Hi Lars,

Before helping, I'd like to thank you for your excellently written
question. You provided a sample source, a sample output and the code
that you'd written so far, showing that you'd thought about the
problem and exactly where you were needed help. This makes it very
easy to understand what you're asking and very easy to answer. I wish
more questions were written like yours.

Anyway, to answer your question:
> During processing, I would like to select all the attr elements of
> the widget that have a "corresponding" attribute in the widget-ref
> element. Alternatively, I would like to select all the attributes of
> the widget-ref elements that have a "corresponding" attr element in
> the widget element. With "corresponding" I mean that the widget-ref
> attribute name is equal to the attr/@name, and as an additional
> constraint, the attr/@public must be "true".

Because the name() function returns the name of only the first node,
it's easiest to try to compare one name() with multiple @names than it
is to try to compare one @name with multiple name()s, so the easiest
way round is to select all the attributes of the widget-ref
elements...

  $widget-ref/@*

whose name() is equal to...

  $widget-ref/@*[name() = ...]

the @name of an attr element under the $widget...

  $widget-ref/@*[name() = $widget/attrs/attr/@name]

with the proviso that you're only interested in attr elements whose
public attribute has the value 'true'...

  $widget-ref/@*[name() = $widget/attrs/attr[@public = 'true']/@name]

To save processing, as they're the same for each $widget-ref
attribute, you should probably store the names of the relevant attr
elements in a separate variable:

  <xsl:variable name="widget-attrs"
                select="$widget/attrs/attr[@public = 'true']/@name" />
  <xsl:for-each select="$widget-ref/@*[name() = $widget-attrs]">
    ...
  </xsl:for-each>
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread