Consider the following XML node:
<Interval>
<P v="1"/>
<Q v="0.0"/>
</Interval>
What is the correct way to pattern match the top level element in Scala? I would expect the following to work but it does not:
def visit(node:Node):String = {
node match {
case p @ <P/> => (p \ "@v") text
case q @ <Q/> => (q \ "@v") text
case <Interval> @ children:_* </Interval> => "parent"
}
}
This expression in scala:
would definitely return a
scala.xml.Nodein scala but firstly ascala.xml.Elem. You can pattern match on it like this:Or you can pattern match on the
childas well, because you pattern match on object n of typeElemis likeElem(n.prefix, n.label, n.attributes, n.scope, n.child)and herechildelements (it's aSeq) are matched against each remaining elements of the pattern:which return
1here for instance.Hope it helps.