Skip to content

SI-9047: Extract attributes from multiple elements #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/main/scala/scala/xml/NodeSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
def \(that: String): NodeSeq = {
def fail = throw new IllegalArgumentException(that)
def atResult = {
lazy val y = this(0)
val attr =
this flatMap (y => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of x map (y => { ... }) you can x map { y => ... }.

if (that.length == 1) fail
else if (that(1) == '{') {
val i = that indexOf '}'
Expand All @@ -106,20 +105,16 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
if (uri == "" || key == "") fail
else y.attribute(uri, key)
} else y.attribute(that drop 1)

attr match {
case Some(x) => Group(x)
case _ => NodeSeq.Empty
}
}
})
}.flatten

def makeSeq(cond: (Node) => Boolean) =
NodeSeq fromSeq (this flatMap (_.child) filter cond)

that match {
case "" => fail
case "_" => makeSeq(!_.isAtom)
case _ if (that(0) == '@' && this.length == 1) => atResult
case _ if that(0) == '@' => atResult
case _ => makeSeq(_.label == that)
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,21 @@ expected closing tag of foo
assertEquals("""<x:foo xmlns:x="gaga"/>""", pp.format(x))
}

@UnitTest
def issueSI9047AttributeFromSingleChildElementWorks: Unit = {
val x = <x><a b='1'/></x>

val b = x \ "a" \ "@b"

assertEquals(List("1"), b map (_.text))
}

@UnitTest
def issueSI9047AttributeMultipleChildElementsWorks: Unit = {
val x = <x><a b='1'/><a b='2'/></x>

val b = x \ "a" \ "@b"

assertEquals(List("1", "2"), b map (_.text))
}
}