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 1102393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:10:46+00:00 2026-05-17T01:10:46+00:00

I saw the following example disccussed here previously, where the goal was to return

  • 0

I saw the following example disccussed here previously, 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) 

I am using Scala 2.7 and everytime the return printed value is always empty. Anyone could help on that ?
Sorry if I am copying another thread… but thought it would be more visible if I posted a new one ?

  • 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-17T01:10:46+00:00Added an answer on May 17, 2026 at 1:10 am

    According to Node ScalaDoc, attribute is defined as follows:

     def attribute(key: String):Option[Seq[Node]]
    

    Therefore, you should modify your code that way:

    def attributeEquals(name: String, value: String)(node: Node) =  
    {  
        node.attribute(name).filter(_.text==value).isDefined // *text* returns a text representation of the node 
    } 
    

    But why not just achieving the same simpler:

    scala> (xml descendant_or_self) filter{node => (node \ "@class").text == "test"}
    res1: List[scala.xml.Node] = List(<span class="test">hello</span>, <div class="test"><p>hello</p></div>)
    
    • 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 on Nabble , where the goal was to return
I saw an example that uses the following to create a point that will
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 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
I saw the following example in book of the C++ Programming Language class Ptr
I saw the following example in Rich's video on sequences http://blip.tv/file/734409 about 33-36 minutes
I saw the following example on the internet: public class TwoThreads { public static

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.