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

pattern matching with namespaces and another question

4 replies
Alexandre Martins
Joined: 2009-04-24,
User offline. Last seen 3 years 24 weeks ago.

Hi

In scala, since one can do pattern matching with XML literals, one
would assume that it is possible to do pattern matching with XML
literals with a namespace. At least I assumed that, wrongly.
to see what I mean check these two examples:

Example 1: without namespaces
scala> val htm = aaa
htm: scala.xml.Elem = aaa

scala> htm match { case {a} => true; case _ => false;}
res19: Boolean = true

Example 2: with namespaces it does not work
scala> val htm = aaa
htm: scala.xml.Elem = aaa

scala> htm match { case {a} => true; case _ =>
false;}
:1: error: in XML literal: '>' expected instead of 'x'
htm match { case {a} => true; case _ =>
false;}
^

I'm trying to work around this by using the scala.xml.Elem contructor
directly but it would be so much more convenient if I could use
scala's XML literals.
Am I doing anything wrong?

Best regards
Alexandre Martins

Alexandre Martins
Joined: 2009-04-24,
User offline. Last seen 3 years 24 weeks ago.
Re: pattern matching with namespaces and another question

Forgot the "another" question.
Sleep deprivation can do that to you :)
Is there a simple way to do a copy of a XML and only
changing/adding/removing the namespace in the XML tree?

2009/7/13 Alexandre :
> Hi
>
> In scala, since one can do pattern matching with XML literals, one
> would assume that it is possible to do pattern matching with XML
> literals with a namespace. At least I assumed that, wrongly.
> to see what I mean check these two examples:
>
> Example 1: without namespaces
> scala> val htm =     xhtml="http://www.w3.org/1999/xhtml">aaa
> htm: scala.xml.Elem = aaa
>
> scala> htm match { case {a} => true; case _ => false;}
> res19: Boolean = true
>
> Example 2: with namespaces it does not work
> scala> val htm =     xhtml="http://www.w3.org/1999/xhtml">aaa
> htm: scala.xml.Elem = aaa
>
> scala> htm match { case xmlns="http://www.w3.org/1999/xhtml">{a} => true; case _ =>
> false;}
> :1: error: in XML literal: '>' expected instead of 'x'
>       htm match { case xmlns="http://www.w3.org/1999/xhtml">{a} => true; case _ =>
> false;}
>                             ^
>
>
> I'm trying to work around this by using the scala.xml.Elem contructor
> directly but it would be so much more convenient if I could use
> scala's XML literals.
> Am I doing anything wrong?
>
> Best regards
> Alexandre Martins
>

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: pattern matching with namespaces and another question

Alexandre wrote:
> I'm trying to work around this by using the scala.xml.Elem contructor
> directly but it would be so much more convenient if I could use
> scala's XML literals.
> Am I doing anything wrong?
>
Unfortunately, the only thing you're doing wrong is expecting this to
work. :/

This topic comes up frequently and I wish I could say that progress was
likely to happen soon... Even before anyone can take a crack at
implementation, we need a concrete proposed syntax and semantics for
match expressions involving namespaces. Since I seem to be the guy who
always says "I think it should look/work like this" every time this
topic comes up, I suspect this is my job. :) Very little time though. :/

The best first step is to file an enhancement request in trac, which
I've done: https://lampsvn.epfl.ch/trac/scala/ticket/2156

-0xe1a

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: pattern matching with namespaces and another question

Alexandre wrote:
> Forgot the "another" question.
> Sleep deprivation can do that to you :)
> Is there a simple way to do a copy of a XML and only
> changing/adding/removing the namespace in the XML tree?
>
http://www.scala-lang.org/docu/files/api/scala/xml/transform/RuleTransfo...
is probably a good place to start...

-0xe1a

Alexandre Martins
Joined: 2009-04-24,
User offline. Last seen 3 years 24 weeks ago.
Re: pattern matching with namespaces and another question

Hi again

> This topic comes up frequently and I wish I could say that progress was
> likely to happen soon... Even before anyone can take a crack at
> implementation, we need a concrete proposed syntax and semantics for match
> expressions involving namespaces.  Since I seem to be the guy who always
> says "I think it should look/work like this" every time this topic comes up,
> I suspect this is my job. :)  Very little time though. :/
I can understand that. I'm sorry I cannot help here but I'm still not
knowledgeable enough to be useful.

> The best first step is to file an enhancement request in trac, which I've
> done: https://lampsvn.epfl.ch/trac/scala/ticket/2156

Up voted it already.

Thanks
Alexandre Martins

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