Groups | Blog | Home
all groups > dotnet xml > june 2004 >

dotnet xml : Value of 'first parent with a value'?


darrel
6/14/2004 2:27:36 PM
Is there a way in XSLT to write this XSL:Value-of select statement (which
I've written in plain english):

value of the attribute of the first ascendent to have a value.

In otherwords, I have an element, and I want to get the parent attribute
that contains an actual value up the chain.

This might be the immediate parent's attribute, the grandparent's attribute
or the great grandparent's attribute (max of 3 levels).

Is there away to do this in the value-of statement, or do I need to call a
template with a series of choose statements?

-Darrel

darrel
6/14/2004 2:46:38 PM
[quoted text, click to view]

Well, this is what I ended up doing:

if test ../@atribute != ''
value of ../@attribute
/if
if test ../../@atribute != ''
value of ../../@attribute
/if
if test ../../../@atribute != ''
value of ../../../@attribute
/if

a bit wordy, but so is xslt. If there's a slicker way to do this, please
share ;o)

-Darrel

Oleg Tkachenko [MVP]
6/15/2004 9:40:00 AM
[quoted text, click to view]

Don't blame XSLT. And after all that's XPath, which is completely
differently colored horse :)

[quoted text, click to view]

ancestor::*[@attribute!=''][1]/@attribute

It selects first ancestor element (in reverse document order as
ancestor:: is reverse axis), which has nonempty @attribute and then
selects that attribute.

Another approach (not so effective though as it involves sorting):

(ancestor::*/@attribute[.!=''])[last()]

It selects all nonempty @attribute on ancestor elements and then takes
last one in document order - the first one in reverse document order.

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button