- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why can't I match an XML node sequence?
Sat, 2009-12-05, 06:11
I am trying to match a sequence of XML nodes, like this:
fragment match { case {_*}{_*} => true ; case _ => false }
In the REPL, I get an error
:1: error: '=>' expected but $XMLSTART$< found.
Wouldn't this be a reasonable thing to do? (It seems reasonable in the
application I am writing :-))
In a similar vein, I have a fragment of type NodeSeq that sometimes
matches {_*} (i.e. a single Elem "a"), and I'd love to be able
to use
fragment match { case {_*} => ... }
instead of first checking that fragment has length 1 and digging out
its only element.
So, I suppose what I am asking is why matching only works for nodes
and not for node sequences.
Cheers,
Cay
Sat, 2009-12-05, 19:17
#2
Re: Why can't I match an XML node sequence?
Well, what is the type of fragment? Perhaps it is an XML Group, in which case you just have to enclose the fragment inside one.
The problem here is that you have two literals. One Node which is an Elem for <a>, and another Node which is an Elem for <b>. What's the container of those literals? You are missing it in the match.
On Sat, Dec 5, 2009 at 3:11 AM, Cay Horstmann <cay.horstmann@gmail.com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
The problem here is that you have two literals. One Node which is an Elem for <a>, and another Node which is an Elem for <b>. What's the container of those literals? You are missing it in the match.
On Sat, Dec 5, 2009 at 3:11 AM, Cay Horstmann <cay.horstmann@gmail.com> wrote:
I am trying to match a sequence of XML nodes, like this:
fragment match { case <a>{_*}</a><b>{_*}</b> => true ; case _ => false }
In the REPL, I get an error
<console>:1: error: '=>' expected but $XMLSTART$< found.
Wouldn't this be a reasonable thing to do? (It seems reasonable in the
application I am writing :-))
In a similar vein, I have a fragment of type NodeSeq that sometimes
matches <a>{_*}</a> (i.e. a single Elem "a"), and I'd love to be able
to use
fragment match { case <a>{_*}</a> => ... }
instead of first checking that fragment has length 1 and digging out
its only element.
So, I suppose what I am asking is why matching only works for nodes
and not for node sequences.
Cheers,
Cay
--
Daniel C. Sobral
I travel to the future all the time.
Mon, 2009-12-07, 05:37
#3
Re: Why can't I match an XML node sequence?
Well, fragment would be a NodeSeq. That's what the Scala XML library
uses for XML fragments. So, my question is why I can't seem to match a
NodeSeq.
Yes, I know, i can put it inside some bogus element, such as
{fragment} match { case
{_*}{_*} => true ; case _ => false }, but
I don't feel I should have to do that.
On Sat, Dec 5, 2009 at 10:03 AM, Daniel Sobral wrote:
> Well, what is the type of fragment? Perhaps it is an XML Group, in which
> case you just have to enclose the fragment inside one.
> The problem here is that you have two literals. One Node which is an Elem
> for , and another Node which is an Elem for . What's the container of
> those literals? You are missing it in the match.
>
> On Sat, Dec 5, 2009 at 3:11 AM, Cay Horstmann
> wrote:
>>
>> I am trying to match a sequence of XML nodes, like this:
>>
>> fragment match { case {_*}{_*} => true ; case _ => false }
>>
>> In the REPL, I get an error
>>
>> :1: error: '=>' expected but $XMLSTART$< found.
>>
>> Wouldn't this be a reasonable thing to do? (It seems reasonable in the
>> application I am writing :-))
>>
>> In a similar vein, I have a fragment of type NodeSeq that sometimes
>> matches {_*} (i.e. a single Elem "a"), and I'd love to be able
>> to use
>>
>> fragment match { case {_*} => ... }
>>
>> instead of first checking that fragment has length 1 and digging out
>> its only element.
>>
>> So, I suppose what I am asking is why matching only works for nodes
>> and not for node sequences.
>>
>> Cheers,
>>
>> Cay
>
>
>
> --
> Daniel C. Sobral
>
> I travel to the future all the time.
>
Mon, 2009-12-07, 15:07
#4
Re: Why can't I match an XML node sequence?
It "would be" or it _is_? It should be simple to find out what type fragment is. If it is a NodeSeq, then this works (tested on 2.7 and 2.8):
fragment match {
case Seq(<a>{_*}</a>, <b>{_*}</b>) => true case _ => false
} There isn't any unapply defined for NodeSeq, but there is one defined for Seq, which NodeSeq extends.
On Mon, Dec 7, 2009 at 2:36 AM, Cay Horstmann <cay.horstmann@gmail.com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
case Seq(<a>{_*}</a>, <b>{_*}</b>) => true case _ => false
} There isn't any unapply defined for NodeSeq, but there is one defined for Seq, which NodeSeq extends.
On Mon, Dec 7, 2009 at 2:36 AM, Cay Horstmann <cay.horstmann@gmail.com> wrote:
Well, fragment would be a NodeSeq. That's what the Scala XML library
uses for XML fragments. So, my question is why I can't seem to match a
NodeSeq.
Yes, I know, i can put it inside some bogus element, such as
<bogus>{fragment}</bogus> match { case
<bogus><a>{_*}</a><b>{_*}</b></bogus> => true ; case _ => false }, but
I don't feel I should have to do that.
On Sat, Dec 5, 2009 at 10:03 AM, Daniel Sobral <dcsobral@gmail.com> wrote:
> Well, what is the type of fragment? Perhaps it is an XML Group, in which
> case you just have to enclose the fragment inside one.
> The problem here is that you have two literals. One Node which is an Elem
> for <a>, and another Node which is an Elem for <b>. What's the container of
> those literals? You are missing it in the match.
>
> On Sat, Dec 5, 2009 at 3:11 AM, Cay Horstmann <cay.horstmann@gmail.com>
> wrote:
>>
>> I am trying to match a sequence of XML nodes, like this:
>>
>> fragment match { case <a>{_*}</a><b>{_*}</b> => true ; case _ => false }
>>
>> In the REPL, I get an error
>>
>> <console>:1: error: '=>' expected but $XMLSTART$< found.
>>
>> Wouldn't this be a reasonable thing to do? (It seems reasonable in the
>> application I am writing :-))
>>
>> In a similar vein, I have a fragment of type NodeSeq that sometimes
>> matches <a>{_*}</a> (i.e. a single Elem "a"), and I'd love to be able
>> to use
>>
>> fragment match { case <a>{_*}</a> => ... }
>>
>> instead of first checking that fragment has length 1 and digging out
>> its only element.
>>
>> So, I suppose what I am asking is why matching only works for nodes
>> and not for node sequences.
>>
>> Cheers,
>>
>> Cay
>
>
>
> --
> Daniel C. Sobral
>
> I travel to the future all the time.
>
--
Daniel C. Sobral
I travel to the future all the time.
Cay Horstmann schrieb:
> Wouldn't this be a reasonable thing to do?
All you need is popular demand and a correct implementation:
http://www.scala-lang.org/node/122#GeneralRegExppatternstemporarilyretra...
- Florian