[xquery-talk] last possible child's attribute

Jens Teubner jens.teubner at inf.ethz.ch
Sun Mar 8 14:58:21 PST 2009


Most XQuery processors won't serialize attributes as top-level items in
a query result (how should a standalone attribute be serialized?).  Try
to wrap your expression into an element constructor:

  element result {
    <insert your path expression here>
  }

Jens

On Sun, Mar 08, 2009 at 02:45:34PM +0100, Michalmas wrote:

> Hi,
> 
> It still gives me the same error:
> 
> XQuery Serialization Error!
> > A document node may not have an attribute node or a namespace node as a
> > child
> >
> 
> @Ken:
> Yes, i meant last possible node that has a value for given attribute.
> 
> On Sun, Mar 8, 2009 at 2:32 PM, G. Ken Holman <gkholman at cranesoftwrights.com
> > wrote:
> 
> > 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:
> >>
> >> <a>
> >>  <aa lc=1>
> >>     <aaa lc=00>
> >>     </aaa>
> >>  </aa>
> >>  <bb lc=0>
> >>  </bb>
> >>  <zz lc=1>
> >>     <ccc lc=123>
> >>     </ccc>
> >>  </zz>
> >> </a>
> >>
> >> 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:
> >
> >  a/descendant::*[last()]/@lc
> >
> > To be concise, you want the last attribute descending from the element:
> >
> >  (a//@lc)[last()]
> >
> > 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
> > <a>
> >  <aa lc="1">
> >     <aaa lc="00">
> >     </aaa>
> >  </aa>
> >  <bb lc="0">
> >  </bb>
> >  <zz lc="1">
> >     <ccc lc="123">
> >     </ccc>
> >  </zz>
> > </a>
> >
> > 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
> > t:\ftemp>
> >
> > --
> > 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
> >
> > _______________________________________________
> > talk at x-query.com
> > http://x-query.com/mailman/listinfo/talk
> >

> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk

-- 
Jens Teubner
ETH Zurich, Systems Group
Haldeneggsteig 4 / IFW B 48.3
8092 Zurich, Switzerland

Software is like sex: It's better when it's free
                               -- Linus B. Torvalds


More information about the talk mailing list