Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 376339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:35:22+00:00 2026-05-12T14:35:22+00:00

I saw the following example on Nabble , where the goal was to return

  • 0

I saw the following example on Nabble, where the goal was to return all nodes that contain an attribute with an id of X that contains a value Y:

//find all nodes with an attribute "class" that contains the value "test"
val xml = XML.loadString( """<div>
<span class="test">hello</span>
<div class="test"><p>hello</p></div>
</div>""" )

def attributeEquals(name: String, value: String)(node: Node) = 
{ 
    node.attribute(name).filter(_==value).isDefined
}

val testResults = (xml \\ "_").filter(attributeEquals("class","test")) 
//prints: ArrayBuffer(
//<span class="test">hello</span>, 
//<div class="test"><p>hello</p></div>
//) 
println("testResults: " + testResults ) 

As an extension to this how would one do the following: Find all nodes that contain any attribute that contains a value of Y:

//find all nodes with any attribute that contains the value "test"
val xml = XML.loadString( """<div>
 <span class="test">hello</span>
 <div id="test"><p>hello</p></div>
 <random any="test"/></div>""" )
 //should return: ArrayBuffer(
 //<span class="test">hello</span>, 
 //<div id="test"><p>hello</p></div>, 
 //<random any="test"/> )

I was thinking I could use a _ like so:

val testResults = (xml \\ "_").filter(attributeEquals("_","test")) 

But it doesn’t work. I know I can use pattern matching, but just wanted to see if I could do some magic with the filtering.

Cheers – Ed

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:35:22+00:00Added an answer on May 12, 2026 at 2:35 pm

    First, XML are literals in Scala, so:

    val xml = <div><span class="test">hello</span><div class="test"><p>hello</p></div></div>
    

    Now, as to the problem:

    def attributeValueEquals(value: String)(node: Node) = {
         node.attributes.exists(_.value.text == value)
    }
    

    In fact, I’d have used “exists” instead of “filter” and “defined” for the original problem as well.

    Finally, I personally prefer operator style syntax, particularly when you have a ready function, instead of an anonymous one, to pass to “filter“:

    val testResults = xml \\ "_" filter attributeValueEquals("test")
    

    The original mixes operator style for “\\” and dot style for “filter“, which ends up quite ugly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw the following example disccussed here previously, where the goal was to return
I saw the following example on the internet: public class TwoThreads { public static
I saw the following example in book of the C++ Programming Language class Ptr
I have the following html code: I saw that for Watir-webdriver the Watir::Image.file_size method
I saw some websites that (for example): if you want to view your message
I saw the following example: Embedding a matplotlib figure inside a WxPython panel I
I was following an example in my book for an AddressCard class. One of
I saw the following code example: Subscription.where(:created_at.gte => t0)` To me, this seems a
I saw the following function call in Yacfe example : Visitor_c.vk_program { Visitor_c.default_visitor_c with
I am following few tutorials for creating MVC3 application. I saw example of adding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.