XPath-like \, node sequences, and attributes

43 views
Skip to first unread message

Cay Horstmann

unread,
Oct 2, 2011, 12:08:45 AM10/2/11
to scala-l...@googlegroups.com
I don't understand how \ works with node sequences and attributes. Consider first

 <a><b/></a><a><b/></a> \ "b" // NodeSeq(<b></b>, <b></b>)

Ok, \ works with node sequences.

And

(<a b="c"/>) \ "@b" // c

Ok, \ works with attributes

But now consider

(<a b="c"/><a b="d"/>) \ "@b" // NodeSeq()

Huh? It doesn't work with attributes and node sequences?

FWIW, \\ works ok:

(<a b="c"/><a b="d"/>) \\ "@b" // NodeSeq(c, d)

Is this a bug or a feature?

Thanks,

Cay

Chris Reeves

unread,
Oct 3, 2011, 1:43:37 AM10/3/11
to scala-l...@googlegroups.com

I could be way off here, but since \ only looks for immediate children and <unnamed_root><a b="c"/><a b="d"/></unnamed_root> has no immediate children (attribute named "b") you get an empty nodeseq. That's based on my assumption that running xpath-like queries on a nodeseq implicitly views the nodeseq as wrapped in an unnamed root element.

That jives a little better with other xml handling libraries than the possible alternative of merging all children of top-level elements into the same pool for the query.

Thanks, Chris

Cay Horstmann

unread,
Oct 3, 2011, 9:41:08 AM10/3/11
to scala-l...@googlegroups.com
I don't think there is an "unnamed root". \ and \\ are defined on NodeSeq.

Chris Reeves

unread,
Oct 3, 2011, 9:50:39 AM10/3/11
to scala-l...@googlegroups.com
In this particular case, I believe the nodeseq itself is the unnamed
root and it's contents are the children. At least that's the only
explanation that makes sense for this behavior, and it conforms to
xml's requirement of a root element.

Thanks, Chris

Alex Cruise

unread,
Oct 4, 2011, 1:07:50 PM10/4/11
to scala-l...@googlegroups.com
On Mon, Oct 3, 2011 at 6:50 AM, Chris Reeves <evsc...@gmail.com> wrote:
In this particular case, I believe the nodeseq itself is the unnamed
root and it's contents are the children. At least that's the only
explanation that makes sense for this behavior, and it conforms to
xml's requirement of a root element.

Yep, without a root element, any notion of correctness is out.  Try wrapping it in <xml:group>_</xml:group> though, that's what Scala uses in this kind of situation.

-0xe1a
Reply all
Reply to author
Forward
0 new messages