RE: [xsl] Position of a Child

Subject: RE: [xsl] Position of a Child
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 21 Sep 2001 11:49:45 -0400
Hi Frank,

At 03:27 PM 9/20/01, you wrote:
The problem with the solution you gave me is that this code is inside a
template (that matches the contextNode), and this template calls itself
recursively for a set of selected attributes.

Basically what I'm doing is to filter the input xml to create an output
based in a list of Childs (that this particular node may have or not).
So going through every child for each one in the list and for each
contextNode would be a lot of processing.

That is why I was using the select"*[name()=$NodeName]" to identify
the child without going through all of them.

It sounds from this that the (count(*[name()=$NodeName]/preceding-sibling::*) + 1) solution would be best. This works on the assumptions that:


- by "position" you mean to count among elements (i.e. there's no mixed-content type stuff with text nodes you have to worry about), and

- you'll never have more than one of a given node, as in

<ContextNode>
        <Child1/>
        <Child3/>
        <Child4/>
        <Child3/>
<ContextNode>

Here, if $NodeName were 'Child3' and you did (count(*[name()=$NodeName]/preceding-sibling::*) + 1) you'd get 5, which isn't what you want. If you know you'll never have more than one Child3, on

<ContextNode>
        <Child1/>
        <Child4/>
        <Child3/>
<ContextNode>

you'd get 3, which is correct.

position() is less useful to you because you have to change context and establish the correct node list in order to be able to use it -- and from what you describe, this could be too busy. (You still might try the other solution though: you might be surprised.)

To get deeper into this, if the count() solution isn't good enough, maybe you could try explaining what your recursive template call on ContextNode is actually doing: maybe there's a more efficient way to get at the problem this is solving, which would alleviate the other problem as well. But I think it should work pretty well for you.

If it doesn't, maybe a two-pass solution, marking each element with its position in pass one, would break the problem down usefully?

Hoping that helps,
Wendell


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