xml - in regards of finding all nodes that have an attribute that matches a certain value with scala -


i saw following example disccussed here previously, goal return nodes contain attribute id of x contains value y:

    //find nodes attribute "class" contains 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 using scala 2.7 , everytime return printed value empty. on ? sorry if copying thread... thought more visible if posted new 1 ?

according node scaladoc, attribute defined follows:

 def attribute(key: string):option[seq[node]] 

therefore, should modify code way:

def attributeequals(name: string, value: string)(node: node) =   {       node.attribute(name).filter(_.text==value).isdefined // *text* returns text representation of node  }  

but why not achieving 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>) 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -