This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: XPath-like \, node sequences, and attributes

3 replies
Chris Reeves
Joined: 2009-10-26,
User offline. Last seen 42 years 45 weeks ago.

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

On Oct 2, 2011 12:08 AM, "Cay Horstmann" <cay [dot] horstmann [at] gmail [dot] com> wrote:
> 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
Cay Horstmann
Joined: 2009-09-04,
User offline. Last seen 42 years 45 weeks ago.
Re: XPath-like \, node sequences, and attributes
I don't think there is an "unnamed root". \ and \\ are defined on NodeSeq.

On Sun, Oct 2, 2011 at 10:43 PM, Chris Reeves <evschris [at] gmail [dot] com> wrote:

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

On Oct 2, 2011 12:08 AM, "Cay Horstmann" <cay [dot] horstmann [at] gmail [dot] com> wrote:
> 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
Joined: 2009-10-26,
User offline. Last seen 42 years 45 weeks ago.
Re: XPath-like \, node sequences, and attributes

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

On Mon, Oct 3, 2011 at 9:41 AM, Cay Horstmann wrote:
> I don't think there is an "unnamed root". \ and \\ are defined on NodeSeq.
>
> On Sun, Oct 2, 2011 at 10:43 PM, Chris Reeves wrote:
>>
>> I could be way off here, but since \ only looks for immediate children and
>> 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
>>
>> On Oct 2, 2011 12:08 AM, "Cay Horstmann" wrote:
>> > I don't understand how \ works with node sequences and attributes.
>> > Consider
>> > first
>> >
>> >
\ "b" // NodeSeq(, )
>> >
>> > Ok, \ works with node sequences.
>> >
>> > And
>> >
>> > () \ "@b" // c
>> >
>> > Ok, \ works with attributes
>> >
>> > But now consider
>> >
>> > (
) \ "@b" // NodeSeq()
>> >
>> > Huh? It doesn't work with attributes and node sequences?
>> >
>> > FWIW, \\ works ok:
>> >
>> > (
) \\ "@b" // NodeSeq(c, d)
>> >
>> > Is this a bug or a feature?
>> >
>> > Thanks,
>> >
>> > Cay
>
>

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: XPath-like \, node sequences, and attributes
On Mon, Oct 3, 2011 at 6:50 AM, Chris Reeves <evschris [at] gmail [dot] 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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland