Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Can I tell scala.xml to match any of two tags?

If you take a look at the source for \ in NodeSeq.scala you can see that it's really just a bit of sugar for a filter operation over descendant_or_self, which is a List[Node], using the node's label.

So you could do the same thing yourself, matching against a set of labels, like this:

val searchedLabels = Set("p", "div")

val results = body.descendant_or_self.filter(node
=> searchedLabels.contains(node.label))

Or if you really want it to seem like "built-in" functionality, you can pimp-on a suitable method to scala.xml.Node like so:

class ExtendedNode(n: Node) {

  def \(labels: Set[String]): NodeSeq = {
    n.descendant_or_self.filter(node =>

implicit def node2extendedNode(n: Node):
ExtendedNode = new ExtendedNode(n)

val results = body \ Set("p", "div")

although I must say I'm not sure I like either the method-name or the use of an implicit here :-(

Categories : Scala

Related to : Can I tell scala.xml to match any of two tags?
Pattern Match on Scala `class`
The argument of unapply should be an instance of the type you want to match: class Foo(val x: Int) object { def unapply(f: Foo): Option[Int] = Some(f.x) }

Categories : Scala
Scala List match last element
You need to switch the order. The xs symbol will eagerly match anything in that position. Trying to match Nil first will make that statement no longer unreachable. Also, you'll still need to match Nil by itself to account for empty lists. def stringify(list: List[String]): String = list match { case x :: Nil => x case x :: xs => x + ":" + stringify(xs) case Nil => "" } Thoug

Categories : Scala
How to match all words in a sentence with scala combinators?
You should use something like that to match sequence of tokens instead of one token: trait TagParser extends RegexParsers { val tags: Parser[List[String]] = rep("""[a-zA-Z]+""".r) } rep is: A parser generator for repetitions. rep(p) repeatedly uses p to parse the input until p fails (the result is a List of the consecutive results of p).

Categories : Scala
Remove prefixed attribute in scala (scala-xml)
You can't remove a prefixed attribute with remove(String) because of its implementation: From Attribute.scala: def remove(key: String) = if (!isPrefixed && this.key == key) next else copy(next remove key) As you can see, if the attribute is prefixed, the first branch condition is false. However, there is a different function in the same class: def remove(namespace: String, scop

Categories : Xml
Scala - Getting the error scala.xml.NodeSeq
You are mixing up the types. Menu takes List[String] and not scala.xml.NodeSeq. You get the error, because you are passing the whole node (scala.xml.NodeSeq), not just the content of it. I can recommend this blogpost to review for further explanation: I would recommend to get your representation right first always. The

Categories : Xml
Recently Add
Reverse list of n elements
scalac: Error: object CharRef in intelliJ 14
Scala - trouble with type inference in lambda expression
Exception on spark test
How can I emulate Haskell's typeclasses in Scala?
Slick: CRUD extension: How to encapsulate implicit mapping:BaseColumnType[T]
Can't find Traverse for sequencing Seq[ValidationNel[String, MyCaseClass]] => ValidationNel[String, Seq[MyCaseClass]]
Tail recursion: internal "loop" function or default values for accumulators
Scala - Add element:MyType to Array = option[MyType] expected
Scala, Composing Function with two values
Using dependent type to generate compile error
How to match all words in a sentence with scala combinators?
Parser Alternative Operator | Fails
ScalaTest assertion mismatch due to Physical Address
Scala implicit parameter and japanese smiley 'foldLeft'
Is it possible to user reduceByKey((x, y, z) => ...)?
How to implement security Authorization using scala and play?
SSO login using scala script
Sum elements based on a predicate
Keep track of completed Futures
API Observable with dynamic caching Remotely closed in gatling
Scala permutations using two lists
Is Scala Either really a Monad
Spark: Use of distinct
Identifying two type wildcards as identical
how to package spark scala application
Pattern Match on Scala `class`
Type mismatch when using higher-kinded types
Scala List match last element
© Copyright 2017 Publishing Limited. All rights reserved.