2

I am trying to validate stuff against an xml schema. I am getting an input from user and checking to see if that value exist as a value for the name attribute. If it does i am returning true or if not false. Ive loaded the schema into a val and selected the nodes that i want like

val a = XML.load("schema.xml")
val nodes = (a \\ "Items")

Now a has the following

<Items name="name1" type="type1" />
<Items name="name2" type="type2" />
<Items name="name3" type="type3" />
<Items name="name4" type="type4" />

The user inputs the value name1

I tried doing

nodes.foreach(checkattr(_))

checkattr(val : elem)
{
  if(elem@name == userinput) { true } else { false }
}

But this is nt working and even if it does it seems the wrong way to do it as i want to return true or false only once for the entire traversal instead of the likes of waht i have specified above.

Any pointers would be useful for me.

2 Answers 2

4

I believe what you want is something like:

scala> <foo baz="quux"/> \ "@baz"
res0: scala.xml.NodeSeq = quux

scala> 
Sign up to request clarification or add additional context in comments.

2 Comments

what happens if there are multiple baz attributes, i tried it and it only returns a null array
@swordfish - Valid XML allows only a single attribute by a given name, doesn't it?
1

This is what I was searching for:

val isThere = dataItems \\ "@name" find { _.text == name }
if (isThere != None)
  return true
else
  false

Posted it as it might be useful for some one else too

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.