RE: [xsl] getting the text nodes from a set of attribute nodes

Subject: RE: [xsl] getting the text nodes from a set of attribute nodes
From: "Robert P. J. Day" <rpjday@xxxxxxxxxxxxxx>
Date: Thu, 4 Sep 2003 12:38:02 -0400 (EDT)
On Thu, 4 Sep 2003 Jarno.Elovirta@xxxxxxxxx wrote:

> Hi,
> 
> >   <a>
> >    <b attr='whatever' ... </b>
> >    <b attr='more' ... </b>
> >    <b attr='yadda yadda' ... </b>
> >   </a>
> > 
> > and i'd like to use another template to process the (string)
> > values of the (set of) "attr" attributes.
> > 
> >   if i'm already processing the <a> element, i can certainly
> > create a node-set of the appropriate attribute nodes with:
> > 
> >  <xsl:variable name="set" select="b/@attr"/>
> > 
> > that will give me a node-set of attribute nodes and, when i
> > pass these to another template, that template will be responsible
> > for taking the string value of each attribute node to do further 
> > processing.
> > 
> >   on the other hand, would it be any faster or more elegant to
> > use an expression to create a node-set of text nodes corresponding
> > to those attribute values right off the bat so that all i'm
> > passing is a set of text nodes?
> 
> Unlike in DOM, XPath attributes don't contain their textual value as a
> text node child, but rather it's just their value, see
> <http://www.w3.org/TR/xpath#attribute-nodes>. So the only way is to pass
> the attribute nodes themselves.

i thought as much, i just wasn't sure i was missing something obvious.
just to explain what my motivation is, i'm trying to fix a bug in
docbook stylesheets, which requires me to do the following.  given
the XML:

  <a>
   <b attr="w1 w3 w6">...</b>
   <b attr="w2 w12 w3 w7">...</b>
   ... more <b>s here ...
  </a>

when processing an <a> element, i need to calculate the maximum
number of whitespace-separated words for any "b/@attr" attribute.
so just what you see above, the value would be 4, based on that
second <b> child of <a>.

AFAICT, this will involve three steps:

1) collect the "b/@attr" attributes (easy)
2) normalize space and word count each of those attribute values
   (again, easy, stealing from kay, p. 527, the "word-count"
   template :-)
3) finding the maximum of those values

since, as i understand it, you can't have a node-set of just numbers,
i'm assuming that i'll just have to do steps 2) and 3) recursively.

no, i don't want the solution, i need the challenge and the practice,
unless there's a clever feature of XSLT i would be overlooking to
do this elegantly.

rday

p.s.  i am curious about the cute way of finding the minimum of 
a set of nodes shown in mangano, p. 63:

  <xsl:value-of select="$nodes[not($nodes &lt; .)]" />

what puzzles me is that, while this is clearly processing a node-set,
you can't (IIRC) have a node-set of just numbers.  so whatever the
types of the nodes in that node-set, they must be automatically
converted to numbers, yes?  so they might be text nodes whose
string values correspond to numeric values, or something like that.



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


Current Thread