[xquery-talk] last possible child's attribute

G. Ken Holman gkholman at CraneSoftwrights.com
Sun Mar 8 09:32:11 PST 2009

At 2009-03-08 13:43 +0100, Michalmas wrote:
>What i need to get in xquery is the last possible child's attribute.

It looks to me like you need the last possible descendant's 
attribute, not child.

>Let's say i have following XML:
>   <aa lc=1>
>      <aaa lc=00>
>      </aaa>
>   </aa>
>   <bb lc=0>
>   </bb>
>   <zz lc=1>
>      <ccc lc=123>
>      </ccc>
>   </zz>
>and i want to get the last child of 'a' node. So, in this case, it 
>would be node 'ccc'. Then, i want to get lc attribute - in this example, 123.

Two ways you could express it, based on how easy you think each will 
be maintained by someone reading your code:

To be explicit, you want the attribute of the last descendant of the element:


To be concise, you want the last attribute descending from the element:


The code below shows both of those working ... and I doubt there 
would be any difference in execution time ... choose whichever one 
"reads" better from a maintenance perspective.

I believe maintenance of transforms is as important as performance 
... let the processor worry about the optimization of the performance.

BTW, I'm assuming you know the attribute's name.  There is no such 
concept as "last specified attribute" for a given element, because 
attributes along the attribute axis are in an arbitrary order, they 
are not in specified order.  I find many students assume that just 
because they specified attributes in a particular order they are 
going to find them in that order when they walk the attribute 
axis.  XML says that attributes are unordered.  In the data model, 
they have an order, you just don't know what that order is.  So you 
can reliably walk over an attribute axis multiple times in one 
transformation and get the attributes in the same order each time 
during that transformation, but they won't necessarily be in that 
order the next time or with another processor.

I hope this helps.

. . . . . . . . . . Ken

t:\ftemp>type michalmas.xml
   <aa lc="1">
      <aaa lc="00">
   <bb lc="0">
   <zz lc="1">
      <ccc lc="123">

t:\ftemp>type michalmas.xq
string( a/descendant::*[last()]/@lc ),
string( (a//@lc)[last()] )
t:\ftemp>xquery michalmas.xml michalmas.xq
<?xml version="1.0" encoding="UTF-8"?>123 123

XQuery/XSLT training in Prague, CZ 2009-03 http://www.xmlprague.cz
XQuery/XSLT/XSL-FO training in Los Angeles/Anaheim - 2009-06-01/10
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman at CraneSoftwrights.com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/q/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

More information about the talk mailing list